Your browser (Internet Explorer 6) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.
X
Post

Custom Button in NavBar

Just a quick snippet of code for you guys today.

Ever wanted to know how to put a custom image into the navigation bar? It’s actually pretty easy. Create a UIButton object, and stick that into the UIBarButtonItem object. The trick is to remember to set the size:

UIButton *add = [UIButton buttonWithType:UIButtonTypeCustom];

image = [UIImage imageNamed:@"button_plus.png"];
[add setImage:image forState:UIControlStateNormal];
image = [UIImage imageNamed:@"button_plus_selected.png"];
[add setImage:image forState:UIControlStateSelected];

add.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
[add addTarget:self action:@selector(onAdd) forControlEvents:UIControlEventTouchUpInside];

addButton = [[UIBarButtonItem alloc] initWithCustomView:add];
self.navigationItem.rightBarButtonItem = addButton;

And Bob’s your uncle!

Leave a comment  

name*

email*

website

Submit comment