简体   繁体   English

如何对所有UIViewController使用相同的UINavigationBar

[英]How to use same UINavigationBar for all UIViewControllers

In my app, I have to show same NavigationBar for all UIViewControllers . 在我的应用中,我必须为所有UIViewControllers显示相同的NavigationBar This NavigationBar has three buttons and these three buttons which will be act as TabBar functionality, that is each tab has its own stack cycle. NavigationBar具有三个按钮,这三个按钮将充当TabBar功能,即每个选项卡都有其自己的堆栈周期。 I have created custom view for NavigationBar with three buttons, but after adding this custom view to HomeViewController, I have to manually add this custom view for all other view controllers. 我已经使用三个按钮为NavigationBar创建了自定义视图,但是在将该自定义视图添加到HomeViewController之后,必须为所有其他视图控制器手动添加该自定义视图。 I don't want to do this. 我不想这么做

Is there any simple method to achieve this? 有没有简单的方法可以做到这一点?

There are a couple of ideas that come to mind. 我想到了几个想法。 First of all, you could use view controller containment and actually have 1 controller that implements your custom nav bar, and then swap out the contained controller as necessary. 首先,您可以使用视图控制器包含 ,实际上有1个控制器实现您的自定义导航栏,然后根据需要换出包含的控制器。

If that's not feasible, you can simply use inheritance and have all your custom controllers inherit from a controller that has the nav bar in place. 如果这不可行,则可以简单地使用继承,并使所有自定义控制器从具有导航栏的控制器继承。

Another option could be to write your own UINavigationController subclass. 另一种选择是编写自己的UINavigationController子类。 I'm not certain if you can override the UNavigationItem behavior, but if you can, you can just just do that -- instead of the UINavigationController taking its child's UINavigationItem to update its own UINavigationBar, the UINavigationBar perhaps just stays the same, like you're expecting/hoping. 我不确定是否可以覆盖UNavigationItem行为,但可以的话,您可以做到这一点-代替UINavigationController接收其子代的UINavigationItem来更新其自己的UINavigationBar,UINavigationBar可能像您一样保持不变期待/希望。

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

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