UIToolbar in Your iPhone Apps

March 27, 2012 at 10:58 AM | Posted in iOS | Leave a comment
Tags: , , , ,

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. Here is an example of what a toolbar will look like in an iPhone app:

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.

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

 UIBarButtonItem *systemItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction

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

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

 //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];


Leave a Comment »

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: