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:
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:
#import "Toolbar.h"
@implementation Toolbar
UILabel *label;
UIToolbar *toolbar;
- (void)viewDidLoad {
[super viewDidLoad];
label = [[UILabel alloc] init];
label.frame = CGRectMake(10, 10, 300, 40);
label.textAlignment = UITextAlignmentCenter;
label.text = @"Press Button";
[self.view addSubview:label];
[label release];
toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleDefault;
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0, 410, 320, 50);
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:)];
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
NSArray *items =
[NSArray arrayWithObjects: systemItem1, flexItem, systemItem2, flexItem, systemItem3, nil];
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[flexItem release];
[toolbar setItems:items animated:NO];
[self.view addSubview:toolbar];
}
- (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!