UINavigationBarItem以编程方式 [英] UINavigationBarItem programmatically
问题描述
我有从 UINavigationController
继承的视图控制器。
在 viewDidLoad
上,我要设置 rightBarButtonItem
。
我会关注。
I have view controller which is inherited from UINavigationController
.
On viewDidLoad
I want to set rightBarButtonItem
.
I do following.
UINavigationItem* navItem = self.navigationItem;
UIButton* btnOptionsView = [[UIButton alloc] init];
[btnOptionsView setImage:[UIImage imageNamed:@"options.png"]
forState:UIControlStateNormal];
[btnOptionsView sizeToFit];
UIBarButtonItem* btnOptions = [[UIBarButtonItem alloc] initWithCustomView:btnOptionsView];
navItem.rightBarButtonItem = btnOptions;
但该按钮不可见,在调试模式下,我注意到 btnOptions。 width
为0,而且我无法为 navItem
设置标题,诸如此类
but the button is invisible, in debug mode I have noticed that btnOptions.width
is 0, and also I can't set the title for navItem
, something like this
[navItem setTitle:@"title"]
不会更改 UINavigationItem
的标题。
推荐答案
子类化UINavigationController。从苹果文档中获取:
You should not subclassing UINavigationController. From apple documentation:
UINavigationController类实现了专用视图
控制器,该控制器管理分层内容的导航。该
类不适用于子类化。而是在希望应用程序的用户
接口反映内容的层次结构的情况下,按原样使用
实例。
The UINavigationController class implements a specialized view controller that manages the navigation of hierarchical content. This class is not intended for subclassing. Instead, you use instances of it as-is in situations where you want your application’s user interface to reflect the hierarchical nature of your content.
这篇关于UINavigationBarItem以编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!