Search code examples
iosobjective-cuinavigationbaruibarbuttonitemuitoolbar

Customizing UIBarButtonItems in various parts of applications (background image and shape)


I want to change the background image of my UIBarButtonItems. In the root view, I want them to be a certain background image (but still rounded-rect buttons) in the nav bar, but in the next view, I have a UIToolBar where I want it to have different backgrounds yet again.

I used [[UIBarButtonItem] appearance] in my app delegate to change all of them, but I now realize for some I want it one style and for others yet another.

More importantly, I want to change not only the background image, but the shape for some. For one of the UIToolBar's UIBarButtonItems I want it to be in the shape of a back button in the navigation bar. How would I achieve this look?

Can I achieve both of these with the method outlined here?

Basically: How do I make custom UIBarButtonItems all over the app, and have some with different shapes?


Solution

  • If you want the buttons to look different in different view controllers, you should set them within the view controller, not in the delegate. Add code in either the init method or the viewWillAppear: method to customise the buttons. If you are customising UIToolbar items then use code similar to the link you gave. If it's for the navigation bar, do something like this:

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //Customise button with background etc that you want
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = item;
    //Release item if you're not using ARC