SpyreStudios

Web Design and Development Magazine

  • Design
  • Showcase
  • Inspirational
  • Tutorials
  • CSS
  • Resources
  • Tools
  • UX
  • More
    • Mobile
    • Usability
    • HTML5
    • Business
    • Freebies
    • Giveaway
    • About SpyreStudios
    • Advertise On SpyreStudios
    • Get In Touch With Us

How to use Custom UIButton Graphics for iPhone Applications

March 2, 2012 by Jake Rocheleau

Anyone familiar with Xcode will likely understand the breadth and depth of the Cocoa Touch library. This mobile development suite includes pre-built components for all of the native iOS functionality such as input fields, switches, sliders, and definitely buttons.

Featured Image: Xcode custom buttons for iPhone apps

In this tutorial I’d like to show you a simple way to implement your own custom button graphics. I’ll be using the latest Xcode 4.2 release and building an application in iOS5. You do not need to understand Objective-C but it certainly doesn’t hurt the process. When building iPhone apps you’ll eventually have to deal with code so Objective-C should be something to embrace!

Picking out some Buttons

To start we should find the perfect graphics to use in our application. There are literally dozens and dozens of freebie-type websites out there where designers will post their work for download. I’m going through 365psd which has some of the best PSD resources for mobile designers.

If you have the graphics skills to make your own buttons then I say go for it! Yet this tutorial focuses around Xcode so I’m using pre-built graphics to save time. As a suggestion though I recommend scaling your graphics as vector artwork. You’ll need a retina display size for your button which is double the original dimensions.

Cloudy user interface Photoshop graphics kit

I’m working with the Cloudy UI buttons created by Sebastien Gabriel. I’ve basically scrubbed all the text off and re-sized for two different scales (standard and retina displays). I also created two sets of buttons – one normal and one highlighted for when the user taps down. Altogether this comes to four files which you can download here.

Building a New Project

By now you should be familiar with creating a new project in Xcode. But to reiterate hit File -> New -> New Project. From the templates list select Single View Application. Then give your app a name and bundle identifier. We actually don’t need a storyboard here whether you use .xib files or not – but leave it checked for now.

On the left side in your Project navigator you should see a folder labeled “Supporting Files”. You want to drag-and-drop your four button files into this group. Notice each of the buttons should be named according to Apple Developer regulations. Here’s the names I have setup:

  • Button.png
  • [email protected]
  • ButtonHighlighted.png
  • [email protected]

When importing the files make sure you select “Create groups for any added folders”. Hit Finish and organize your files if needed.

Coding the View Controller

If you notice in our project navigator there are very few files to work with. Select ViewController.m and look for the function viewDidload. It’s inside here that we’ll setup our buttons and configure them on the view.

[cpp wraplines=”false”] // creating custom button properties
UIFont *buttonFont = [UIFont fontWithName:@"Noteworthy-Bold" size:17.0];
UIColor *buttonColorDefault = [UIColor colorWithRed:90.0f/255.0f green:90.0f/255.0f blue:90.0f/255.0f alpha:1.0];
UIColor *buttonColorHighlight = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1.0];

UIImage *btn = [UIImage imageNamed:@"Button.png"];
UIImage *btnh = [UIImage imageNamed:@"ButtonHighlighted.png"];
[/cpp]

To start I’ve included a few variables we will use on the button styles. I also created two new variables which store a UIImage data type for our two button states (normal and highlighted). Notice we don’t need to declare the @2x retina images because the iOS compiler recognizes to display these by default.

Custom Button Properties

If you are with me so far then let’s just jump right into the button code. This requires us creating a new UIButton and setting the buttonWithType parameter to UIButtonTypeCustom. From here we can also implement further customizations.

[cpp wraplines=”false”]// building the buttons
UIButton *aboutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[aboutBtn addTarget:self action:@selector(showAboutPage) forControlEvents:UIControlEventTouchUpInside];
[aboutBtn setTitle:@"About Me" forState:UIControlStateNormal];
[aboutBtn setFrame:CGRectMake(80.0, 120.0, 162.0, 42.0)];
[aboutBtn setBackgroundImage:btn forState:UIControlStateNormal];
[aboutBtn setBackgroundImage:btnh forState:UIControlStateHighlighted];

[aboutBtn.titleLabel setFont:buttonFont];
[aboutBtn setTitleColor:buttonColorDefault forState:UIControlStateNormal];
[aboutBtn setTitleColor:buttonColorHighlight forState:UIControlStateHighlighted];
[/cpp]

You can probably figure out the common properties in this code block. The button fonts and backgroundImage settings are all using the pre-built variables in our first bit of code. The CGRectMake function is a bit confusing if you aren’t familiar with the syntax.

This basically creates an object to place on the view with pre-determined settings for width, height, and X/Y coordinates. Our first button will read “About Me” and calls out to an empty selector function named showAboutPage. I added that code directly after our viewDidLoad() method(copied below).

[cpp wraplines=”false”]- (void)showAboutPage
{
// use this method to display your about page
// only when the button is tapped and released
}
[/cpp]

Displaying the UIButton

If you go and run the application now nothing will show up. We do have our button programmed and styled just right – however there’s one last line of code we need. After all the lines of button configurations (but still inside viewDidLoad) add this last line of code:

[cpp wraplines=”false”]// place the button into the view
[self.view addSubview:aboutBtn];
[/cpp]

This uses the CGRectMake method to build and display our button of the exact dimensions and X/Y coordinates on the screen. Go ahead and compile the app to notice we have our first button! You can tap and you’ll get the highlighted effect – but there is no actual callback code. You would need to edit the function we made earlier called showAboutPage to push onto a new view controller or whatever action you’d like to happen.

If you are feeling ambitious try copy/pasting the same button codes and create a few more. Remember that you’ll need to change the CGRectMake() function coordinates to move the new button either below or above the first one. You’ll also have to think of a new variable name other than aboutBtn.

However if you still can’t get it work don’t feel discouraged! Xcode is difficult and does take a lot of practice. Download my source code example below and see if you can compare your own project with mine.

Demo Source Code

Custom iPhone UIButton Xcode app - final preview

Conclusion

For anybody interested in building iPhone apps this should be a great introductory tutorial. Learning how to customize iOS user interface elements is crucial to expanding your development knowledge.

Not to mention that Xcode makes a fantastic IDE for coding in Objective-C. You can check through Google for dozens of similar freebies – even more than just button sets. Whole UI kits are often put together and offered for free by talented designers. If you have ideas or questions about the tutorial feel free to share with us in the post discussion area below.

Filed Under: Tutorial

Recent Posts

  • 31 Fresh Design Elements for Spring and Easter
  • 10 Templates for Music Concert Flyers
  • How to Build a Web Scraper Using Node.js
  • Best PHP Books, Courses and Tutorials in 2022
  • How to Get Your First Web Design Client

Archives

  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • May 2008
  • April 2008

Categories

  • Accessibility
  • Android
  • Apps
  • Art
  • Article
  • Blogging
  • Books
  • Bootstrap
  • Business
  • CSS
  • Design
  • Development
  • Ecommerce
  • Fireworks
  • Flash
  • Freebies
  • Freelance
  • General
  • Giveaway
  • Graphic Design
  • HTML5
  • Icons
  • Illustrator
  • InDesign
  • Infographics
  • Inspirational
  • Interview
  • Jobs
  • jQuery
  • Learning
  • Logos
  • Matrix
  • Minimalism
  • Mobile
  • Motion Graphics
  • Music
  • News
  • Photoshop
  • PHP
  • Promoted
  • Rails
  • Resources
  • Showcase
  • Tools
  • Tutorial
  • Twitter
  • Typography
  • Uncategorized
  • Usability
  • UX
  • Wallpapers
  • Wireframing
  • WordPress
  • Work

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

SpyreStudios © 2022