xcode 你能在 UINavigationController 中推送一个 UITabBarController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20822160/
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
Can you push a UITabBarController inside an UINavigationController
提问by Mike Flynn
Is it not possible to push a UITabBarController inside a UINavigationController? I read back in older versions of iOS but is that still the case now?
是否无法在 UINavigationController 中推送 UITabBarController?我在旧版本的 iOS 中读了一遍,但现在仍然如此吗?
回答by n00bProgrammer
Simply putting it, YES you can.
简单地说,是的,你可以。
But simply because you can, does not mean you should. The UITabBarController
is intended to be used as a viewController at the root level (as the rootViewController of the app window). The aim is to provide the user with the best(and easy) UX. Apple advises to keep the view hierarchy of your app such that there is only one 'path' from one view controller to another.
但仅仅因为你可以,并不意味着你应该。所述UITabBarController
的目的是在根级别被用作一个的viewController(作为应用窗口的RootViewController的)。目的是为用户提供最好的(和简单的)用户体验。Apple 建议保持应用程序的视图层次结构,以便从一个视图控制器到另一个视图控制器只有一个“路径”。
The widely accepted way of using a UITabBarController
is to set it as the rootViewController
, and assign UIViewControllers
to each 'tab'. If you want to be able to push/pop from these view controllers, you assign a UINavigationController
to each tab, with their own rootViewController
s instead, just like the Facebook app for iOS.
广泛接受的使用 a 的方法UITabBarController
是将其设置为rootViewController
, 并分配UIViewControllers
给每个“选项卡”。如果您希望能够从这些视图控制器推送/弹出,您可以UINavigationController
为每个选项卡分配一个rootViewController
s,而不是它们自己的s,就像 iOS 的 Facebook 应用程序一样。
If you do decide to push it onto a UINavigationController
it would be advised to use simple viewControllers
in each tab, rather than UINavigationController
s, to avoid multiple view hierarchies. The rest is upto you as a developer. In no scenario, should ease-of-coding affect the end user's experience. The priority is always the latter.
如果您决定将它推到 a 上UINavigationController
,建议viewControllers
在每个选项卡中使用 simple而不是UINavigationController
s,以避免多个视图层次结构。其余的取决于您作为开发人员。在任何情况下,易于编码都会影响最终用户的体验。优先级始终是后者。
EDIT:
编辑:
Create a
UITabBarController
.You set the root view controllers for each tab using:
tabBarController.viewControllers = [NSArray arrayWithObjects: alphaController, betaController, nil];
Push it onto your navigation controller.
In the
viewDidLoad
method of yourUITabBarController
class (advised to subclass), you set it'sUITabBar
's items (UIBarButtonItems
).
创建一个
UITabBarController
.您可以使用以下方法为每个选项卡设置根视图控制器:
tabBarController.viewControllers = [NSArray arrayWithObjects: alphaController, betaController, nil];
将其推到您的导航控制器上。
在
viewDidLoad
你的UITabBarController
类的方法中(建议子类),你设置它的UITabBar
items (UIBarButtonItems
)。
Using UITabBarControllerDelegate
, you can handle their selection. You do not necessarily have to show viewControllers on selection of tabs. You can alter their behaviour by overriding the shouldSelectViewController:
method.
使用UITabBarControllerDelegate
,您可以处理他们的选择。您不必在选择选项卡时显示 viewControllers。您可以通过覆盖该shouldSelectViewController:
方法来改变他们的行为。
回答by Kuba Suder
You can, but since the docs say you shouldn't do that, it can and will cause problems. I've discovered this when I put a tab bar controller inside navigation stack (before I've noticed the info in the docs) and then I noticed that the navigation bar behaves strangely when turning it on an off while switching between tabs. I wanted some of the view controllers inside the tab bar controller to have a visible navigation bar and some don't, but this totally doesn't work as it should (it's like there's a delay and the nav bar hides and shows one tab switch later than it's supposed to). So I'd recommend against it.
你可以,但由于文档说你不应该这样做,它可以而且会导致问题。当我将标签栏控制器放在导航堆栈中时(在我注意到文档中的信息之前)时,我发现了这一点,然后我注意到在标签之间切换时打开和关闭导航栏的行为很奇怪。我希望标签栏控制器内的一些视图控制器具有可见的导航栏,而有些则没有,但这完全不能正常工作(就像有延迟并且导航栏隐藏并显示一个标签开关比预期的要晚)。所以我建议反对它。
回答by Slashik
Apple says NO, but here guide on russian with code p.2 http://arm1.ru/blog/ispolzovanie-uinavigationcontroller-vmeste-s-uitabbarcontroller
苹果说不,但这里有俄语指南,代码 p.2 http://arm1.ru/blog/ispolzovanie-uinavigationcontroller-vmeste-s-uitabbarcontroller
回答by Shahab Qureshi
I was recently working on application in which UITabbarController was being pushed inside a UINavigationController. This application is currently on apple store.
我最近正在研究将 UITabbarController 推入 UINavigationController 的应用程序。此应用程序目前在苹果商店。
I don't know why apple don't recommend this and why people faces problems with this.
我不知道为什么苹果不推荐这个,为什么人们会遇到这个问题。
@Slashik answer is one example, I used the same way.
@Slashik 的回答就是一个例子,我用了同样的方法。