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?
-
2Well, Apple doesn't want you to do this, but it took me about 90 seconds to test it out and find out that you can do it. Whether you should do it is the more important question -- since Apple says you shouldn't do it, I have no idea whether they would approve such a design, or whether you would experience unintended consequences from such a design.– rdelmarDec 29, 2013 at 5:16
-
Im having so much trouble pushing a uitabbarcontroller from the initial view inside a navigation controller. The uitabbarcontroller is just white after being pushed. Did you actually put navigation controllers inside the tabs and have a working uitabbarcontroller?– Mike FlynnDec 29, 2013 at 5:57
5 Answers
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.
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.
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.
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
).
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.
-
I'm sorry I could not add references from Apple's Documentation. For some reason, the developer portal is down on my side. Will add appropriate references as soon as I have access. Dec 29, 2013 at 10:44
Apple says NO, but here guide on russian with code p.2 http://arm1.ru/blog/ispolzovanie-uinavigationcontroller-vmeste-s-uitabbarcontroller
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.
-
"The docs say you shouldn't" - do you know if it is still valid that you should not put UITabBarController inside UINavigationController or other container? Currently I can't find such statement anywhere in the docs. Mar 27, 2018 at 16:31
-
1@LeszekSzary I found it: developer.apple.com/documentation/uikit/uinavigationcontroller/… - "This object cannot be a tab bar controller" Apr 1, 2018 at 15:16
I was recently working on application in which UITabbarController was being pushed inside a UINavigationController. This application is currently on apple store.
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.
I found a case in which the UITabBarController
inside UINavigationController
does not work as you expect. Code to reproduce the error:
ViewController.swift
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(someSelector))
}
}
SceneDelegate.swift
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
window.rootViewController = createRootViewController()
window.makeKeyAndVisible()
self.window = window
}
fileprivate func createRootViewController() -> UIViewController {
let viewController = ViewController()
let tabBarController = UITabBarController()
tabBarController.viewControllers = [
viewController
]
let navigationController = UINavigationController()
navigationController.pushViewController(tabBarController, animated: false)
return navigationController
}
I expect an "add" button to appear in the upper right corner. But in the end, the button is not there. However, if you rewrite the method code createRootViewController
to the following:
let viewController = ViewController()
let tabBarController = UITabBarController()
tabBarController.viewControllers = [
UINavigationController(rootViewController: viewController)
]
return tabBarController
then the "add" button appears.
Therefore, if you do not want to spend a few hours debugging, it is better to stick to their advice. I do not know why the developers of the SDK did not add an assert there.