iosuistoryboarduitabcontroller

Add UINavigationController to UITabBarController without Tab Bar showing up in all views


I currently have Tab Bar Controller with a Navigation Controller, as described here.

As a result, the tab bar shows below each view.

Is there a way to hide the tab bar on consecutive screens, which are not directly connected to the tab bar controller, using Storyboard.

The current flow is pictured here:

storyboard with flow

Example of desired navigation

For example, "Second View" should show with the navigation controller, since it is direct child of Tab Bar Controller.

enter image description here

However, the "Third View" and "Fourth View", should have the navigation controller only (without the tab bar):

enter image description here

Actual (with tab bar) vs. the expected (desired result is the view without the tab bar):

enter image description here

Un-suggested solution

enter image description here

A possible way to get the desired flow is to create a navigation controller then connect it to the Tab Bar Controller. However this is not recommended as a UI pattern by Apple (Apple docs suggest to use the above method), and leads to several subtle bugs:


Solution

  • UIViewController has a property hidesBottomBarWhenPushed. Set it to true in viewDidLoad of ThirdViewController and FourthViewController.

    UPDATE

    Your current hierarchy of view controllers is completely fine. Don't change it to anything else.

    UPDATE 2

    You're right viewDidLoad is not good enough. Use init?(coder:).

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        hidesBottomBarWhenPushed = true
    }
    

    Or set a flag in Interface Builder.

    enter image description here