How to Make a Toolbar with UIToolbar

Toolbars are used to present a set of functions to a user. You will see these at the bottom of iPhone apps and generally give you the option to do things like edit files, send email or take a picture.

Using UIToolbar in Your iPhone Apps

Today, I am going to show you how to use UIToolbar in your iPhone apps. Essentially, what you need to do is to create an instance of UIToolbar, add buttons to it and then add the toolbar to your view. Usually, you will also need to assign actions to each toolbar button as well.

Here is video of how to use UIToolbar taken from my own code library that I ship with my ebook:

Source Code for Using UIToolbar

#import "Toolbar.h"
@implementation Toolbar	UILabel *label;	UIToolbar *toolbar;

-(void)viewDidLoad {
     [super viewDidLoad];

     //Create label
     label = [[UILabel alloc] init];
     label.frame = CGRectMake(10, 10, 300, 40);
     label.textAlignment = UITextAlignmentCenter;
     label.text = @"Press Button";

     [self.view addSubview:label];
     [label release];

     //create toolbar using new
     toolbar = [UIToolbar new];
     toolbar.barStyle = UIBarStyleDefault;
     [toolbar sizeToFit];
     toolbar.frame = CGRectMake(0, 410, 320, 50);

     //Add buttons
     UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                                  target:self
                                                                                  action:@selector(pressButton1:)];

     UIBarButtonItem *systemItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                                                                  target:self
                                                                                  action:@selector(pressButton2:)];

     UIBarButtonItem *systemItem3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
                                                                                  target:self
                                                                                  action:@selector(pressButton3:)];

     //Use this to put space in between your toolbox buttons
     UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                               target:nil
                                                                               action:nil];

     //Add buttons to the array
     NSArray *items = [NSArray arrayWithObjects: systemItem1, flexItem, systemItem2, flexItem, systemItem3, nil];

     //release buttons
     [systemItem1 release];
     [systemItem2 release];
     [systemItem3 release];
     [flexItem release];

     //add array of buttons to toolbar
     [toolbar setItems:items animated:NO];
     [self.view addSubview:toolbar];

}

//Action methods for toolbar buttons:
-(void) pressButton1:(id)sender{
     label.text = @"Add";
}

-(void) pressButton2:(id)sender{
     label.text = @"Take Action";
}

-(void) pressButton3:(id)sender{
     label.text = @"Camera";
}

-(void)dealloc {
     [toolbar release];
     [label release];
     [super dealloc];
}

@end

 

What Functions Do You Think Fit In Well On a Toolbar?

Discuss in the comments below!

6 Responses to How to Make a Toolbar with UIToolbar

  1. iPhoneKicks.com November 10, 2009 at 6:51 pm #

    How to Make a Toolbar with UIToolbar…

    You’ve been kicked (a good thing) – Trackback from iPhoneKicks.com – iPhone SDK links, community driven…

  2. StormRoBoT November 14, 2009 at 5:35 pm #

    Thanks so much :)

  3. Free Collection Of Outstanding July 20, 2010 at 6:47 am #

    [...] A great code tutorial on the UIToolbar for beginners can be found here: How To Make A Toolbar With UIToolbar [...]

  4. KaZuH!KO July 9, 2011 at 7:46 am #

    WoW!!! Great, thank you :-)

  5. Learner July 31, 2011 at 4:34 pm #

    Excellent – just what I was looking for – sample code that is obscured by an advert. I hate it when tutorials are clear, concise, and helpful, and it can be difficult to find a tutorial page that is a cock-up, I've been looking for ages for something written by somebody to bloody lazy to view their own layout, and I'm delighted to say I've found it !. Perhaps you could also do a set of tutorials full of spelling mistakes and code errors, that would be a terrific service.

    • Slowburner September 28, 2011 at 3:29 pm #

      … sounds like Learner was having a difficult day; the sarcasm is so thick I can barely see the web page.

Leave a Reply

Switch to our mobile site