Search code examples
iosios6uinavigationcontrolleruinavigationbaruinavigationitem

Add a custom button to UINavigationController subclass


I am trying to customize the NavigationBar by subclassing the navigation controller. For some reason I can't get the custom button to show up. My setup is pretty straight forward I have a ViewController which is embedded into a Navigation Controller which uses my custom subclass NavController:

@interface NavController ()

@end

@implementation NavController

- (void)viewDidLoad{
    [super viewDidLoad];

    UIImage *wlImage = [UIImage imageNamed:@"wl-icon.png"];

    UIButton *wlButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [wlButton setFrame:CGRectMake(0, 0, wlImage.size.width, wlImage.size.height)];
    [wlButton setBackgroundImage:wlImage forState:UIControlStateNormal];

    UIBarButtonItem *wlButtonItem = [[UIBarButtonItem alloc] initWithCustomView:wlButton];
    wlButtonItem.tintColor = [UIColor blackColor];

    self.navigationItem.rightBarButtonItem = wlButtonItem;
    self.navigationBar.tintColor = [UIColor blackColor];
}

@end

When I try to test the app the only customization that appears to work is the tint. The button won't appear.

enter image description here

I am wondering if it has something to do with the Navigation Item.

Could it be that my Initial View Controller is overriding the Navigation Item within it's own default setup?


Solution

  • While the nav controller has a navigationItem, it's never actually used. You should be overriding the nav controller methods to push view controllers (or using the delegate methods) to instead modify the navigationItem of each new view controller pushed.