Saturday, November 22, 2008

Creating Toolbar in iphone sdk

Here is how you can create a toolbar at the bottom of your screen. (with code taken from the UICatalog sample)

UIToolbar *bar = [UIToolbar new];
bar.barStyle = UIBarStyleBlackOpaque;

// size up the toolbar and set its frame
[bar sizeToFit];
CGFloat toolbarHeight = [bar frame].size.height;
CGRect mainViewBounds = self.view.bounds; //view of your viewcontroller
[bar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
CGRectGetMinY(mainViewBounds) + CGRectGetHeight(mainViewBounds),
CGRectGetWidth(mainViewBounds),
toolbarHeight)];

//add button
// create a bordered style button with custom image
UIImage *im = [UIImage imageNamed:@"selection.png"];
UIBarButtonItem *selectItem = [[UIBarButtonItem alloc] initWithImage:im
style:UIBarButtonItemStylePlain
target:self
action:@selector(changeSelection:)];
selectItem.width = 50.0f; //you can set the width of the icon
selectItem.title= @"Selection"; //set text below image icon

//note the nil at the end
NSArray *items = [NSArray arrayWithObjects: selectItem,nil];
[bar setItems:items animated:NO];
[self.view addSubView:bar];
[selectItem release];
[bar release];

5 comments:

Theresa said...

Great post. Thanks for sharing this Code about to generate toolbar. iphone application development

Blackberry web app development said...

Thanks for sharing these instructions. I have created a customize toolbar with the help of these steps. It’s very easy and I’m using it on my iPhone 3G.

Anonymous said...

Good post. it's really helpful and save time.

Unknown said...

Good post. It's really helpful and saves time.

Unknown said...

Good Post. It's really helpful and saves time as well.

Thanks,
Jaydevsinh gohil