繁体   English   中英

如何根据 UINavigationController 中显示的 UIViewController 更改 UINavigationBar

[英]How to change UINavigationBar depending on the UIViewController shown in UINavigationController

正如问题所说,当 UINavigationController 显示不同的 UIViewController 时,我需要能够更改 UINavigationBar 的内容。 这对于标题很容易实现,因为导航控制器只使用视图控制器的标题属性。 我正在努力根据视图控制器更改左右栏按钮项目。

我用来更改它们的代码已经在其他多个问题中得到解决,并不是真正的问题,尽管作为参考,我将使用以下内容:

UIBarButtonItem *newItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
    UIBarButtonSystemItemAdd target:self action:nil];
newItem.tintColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = newItem;

我想我正在寻找的是一种方法,每次导航控制器显示新的 UIViewController 时都会调用该方法以设置导航控制器布局。 在 UINavigationController 的文档中是以下方法:

// Called when the navigation controller shows a new top view controller via a push, pop or setting of the view controller stack.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;

但是,当我运行应用程序时,似乎在任何时候都不会调用此方法。 那么,当导航控制器显示新的视图控制器时,是否有一个方法被调用? 或者我在这里完全错了,并且已经有一种可以接受的方式来改变导航栏,并且视图显示在它下面,而我迄今为止错过了?

典型的解决方案是将代码放在每个视图控制器的viewDidLoad方法中,以设置所需的按钮。

您问题中的第一部分代码将是视图控制器设置所需按钮的完美示例,该按钮在显示导航栏(视图控制器)时显示在导航栏的右侧。

您无需处理导航控制器委托或导航控制器本身。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM