我有从 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以编程方式
发布评论