问题描述
我试图通过继承导航控制器来自定义NavigationBar。出于某种原因,我无法显示自定义按钮。我的设置很简单我有一个ViewController,它嵌入到导航控制器中,它使用我的自定义子类 NavController :
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?
推荐答案
虽然导航控制器有 navigationItem
,但它实际上从未使用过。您应该重写导航控制器方法以推送视图控制器(或使用委托方法),而不是修改推送的每个新视图控制器的 navigationItem
。
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.
这篇关于向UINavigationController子类添加自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!