Add a custom button to UINavigationController subclass

喜你入骨 提交于 2019-12-11 10:26:35

问题


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.

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?


回答1:


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.



来源:https://stackoverflow.com/questions/17888648/add-a-custom-button-to-uinavigationcontroller-subclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!