简体   繁体   English

在所有视图上添加导航栏。 迅速

[英]Adding navigation bar on all the views. Swift

This is a basic question, but I couldn't really find a answer. 这是一个基本问题,但我找不到答案。
In my swift app, I decide to add navigation bar on all the view controller in storyboard. 在我的swift应用程序中,我决定在故事板中的所有视图控制器上添加导航栏。

Do I have to manually add in the bar for each of them, or is there any clean way I could add them in automatically/programmatically only once? 我是否必须手动添加每个栏中的栏,或者是否有任何干净的方式我可以自动/编程只添加一次?
(I just want to add the bar with title, not even the buttons or the navigation function.) (我只想添加标题栏,甚至按钮或导航功能。)

Thank you in advance! 先感谢您!

Yeah, the Navigation Controller puts a navigation bar on every one in your storyboard. 是的,导航控制器会在故事板中为每个导航栏添加一个导航栏。 Just drag one onto your storyboard, connect your first controller as the root to it, and make every following view controller connected by "Show" segues to the last. 只需将其中一个拖到故事板上,将第一个控制器作为根连接到它,然后通过“Show”将每个后续视图控制器连接到最后一个。 Now all your view controllers should have a navigation bar on top of them. 现在,您的所有视图控制器都应该有一个导航栏。

You can access their navigation controller with this property in a view controller: 您可以在视图控制器中使用此属性访问其导航控制器:

self.navigationController

Take a look at this tutorial , it'll give you more details on how navigation controllers / view controller hierarchy works. 看一下本教程 ,它将为您提供有关导航控制器/视图控制器层次结构如何工作的更多详细信息。

Yes you will have to manually add the custom bar to each of them. 是的,您必须手动将自定义栏添加到每个栏。

However, you can make it cleaner by defining a class for that custom view. 但是,您可以通过为该自定义视图定义类来使其更清晰。 For example HeaderBar then you do all the setup in this class itself ( HeaderBar.swift ). 例如HeaderBar然后你在这个类本身( HeaderBar.swift )中进行所有设置。 When you add this bar to any view controller, all you need to do is simply drag and drop from storyboard and typing class name HeaderBar and it is ready to go, no more further configuration. 当您将此栏添加到任何视图控制器时,您只需从故事板拖放并键入类名称HeaderBar ,它已准备就绪,无需进一步配置。

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

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