ios 如何以正确的方式使用导航控制器实现标签栏控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27422054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement tab bar controller with navigation controller in right way
提问by Matrosov Alexander
I am using Storyboard and Xcode 6. I have next controllers and scenes in my Storyboard:
我正在使用 Storyboard 和 Xcode 6。我的 Storyboard 中有下一个控制器和场景:
UINavigationController
that has HomeViewController
as a root. HomeViewController
has a button that Show (e.g. Push)
UITabBarController
. UITabBarController
has 4 UIViewControllers
.
UINavigationController
有HomeViewController
作为根。HomeViewController
有一个按钮Show (e.g. Push)
UITabBarController
。UITabBarController
有 4 UIViewControllers
。
But my problem that after I Show UITabBarController
there are no Navigation Bars in 4 UIViewControllers
. But I supposed that if I Show (e.g. Push)
UITabBarController
then it should has embedded navigation controller that is initial controller in storyboard. Am I right? And if so how can I setup then navigation bar in Storyboard, because there are now default bar event in pushed tab bar that I see on storyboard. I have selected UIViewController and set simulated metrics in identity inspector to Translucent Navigation bar for the Top property, but I supposed it should be automatically added to this controller and to the tab bar without additional steps.
但是我的问题是在我 Show 之后UITabBarController
4 中没有导航栏UIViewControllers
。但我想,如果我Show (e.g. Push)
UITabBarController
那么它应该有嵌入式导航控制器,它是故事板中的初始控制器。我对吗?如果是这样,我如何在情节提要中设置导航栏,因为现在我在情节提要上看到的推送选项卡栏中有默认栏事件。我选择了 UIViewController 并将身份检查器中的模拟指标设置为 Top 属性的半透明导航栏,但我认为它应该自动添加到这个控制器和标签栏,而无需额外的步骤。
Or should I add new navigation controller for each tab bar items that will have their root view controllers?
或者我应该为每个具有根视图控制器的选项卡栏项添加新的导航控制器吗?
The main question why I don't see navigation bar in storyboard using show (e.g. Push). For example if I add navigation controller and then set as root - tab bar controller then Xcode automatically add top navigation bar, but if the queue has an extra step like in my case HomeViewController the top navigation bar never appear automatically.
主要问题为什么我在使用 show (例如 Push)的故事板中看不到导航栏。例如,如果我添加导航控制器,然后设置为 root - 标签栏控制器,那么 Xcode 会自动添加顶部导航栏,但是如果队列有一个额外的步骤,比如我的情况 HomeViewController,顶部导航栏永远不会自动出现。
回答by App Dev Guy
Hi you need to embed each view controller that is within the tab bar in a navigation controller of its own. So the flow is like so (HomeVC is embedded in a NavController of it's own):
嗨,您需要将标签栏中的每个视图控制器嵌入到自己的导航控制器中。所以流程是这样的(HomeVC 嵌入在它自己的 NavController 中):
/ --> `NavController` --> `ViewController1`
| --> `NavController` --> `ViewController2`
`HomeViewController`-->`TabBarController`|--> `NavController` --> `ViewController3`
\--> `NavController` --> `ViewController4`
- Go to Editor--> Embed In--> Tab Bar Controller(or Navigation Controller)
- 转到编辑器-->嵌入-->标签栏控制器(或导航控制器)
To answer your questions:
回答您的问题:
Each tab of a tab bar controller interface is associated with a custom (different [sic]) view controller. When the user selects a specific tab, the tab bar controller displays the root view of the corresponding view controller, replacing any previous views.
选项卡栏控制器界面的每个选项卡都与自定义(不同 [原文如此])视图控制器相关联。当用户选择特定选项卡时,选项卡栏控制器会显示相应视图控制器的根视图,替换之前的所有视图。
So the Root View Controller of the tab must be adjoined to a Navigation Controller; a navigation view controller must be next inline in order for the View Controller to inherit a Navigation. A Tab Bar switches views to whatever is next inline.
所以选项卡的根视图控制器必须与导航控制器相邻;导航视图控制器必须是下一个内联,以便视图控制器继承导航。标签栏将视图切换到下一个内联内容。
This document will help outline more information about it. https://developer.apple.com/documentation/uikit/uitabbarcontroller
本文档将有助于概述有关它的更多信息。https://developer.apple.com/documentation/uikit/uitabbarcontroller
回答by Brian Ogden
In Swift 2, Xcode 7 has a very handy feature for adding a UINavigationController:
在 Swift 2 中,Xcode 7 有一个非常方便的功能来添加 UINavigationController:
- Select the UIViewController that is being used as a "tab" for the
UITabBarNavigationController
- On the top Xcode menu, select "Editor" ->
- "Embed In" ->
"Navigation Controller"
回答by Pixki
If you want to have something like that:
如果你想拥有这样的东西:
TabBarController -> Navigation Controller -> View Controller with a Table View -> and from the TableView a MasterDetailView
for example:
TabBarController -> Navigation Controller -> View Controller with a Table View -> and from the TableView a MasterDetailView
例如:
I had the problem that there were no Navigation in the MasterDetailView (no Back Button to the ViewController with The TableView).
我遇到的问题是 MasterDetailView 中没有导航(没有返回按钮到带有 TableView 的 ViewController)。
Workaround is:
解决方法是:
Set Segue between TableView and MasterDetailView to: Kind: Push (Deprecated)
将 TableView 和 MasterDetailView 之间的 Segue 设置为: Kind: Push(已弃用)
Run your app...hopefully you will see the Back Button...change the Kind to Show (e.g. Push)
, run again -> it should work.
运行您的应用程序...希望您会看到后退按钮...将 Kind 更改为Show (e.g. Push)
,再次运行 -> 它应该可以工作。