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:
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