UITabBarController inside master of UiSplitViewController
Asked Answered
P

1

10

I want to achieve the same flow as Facebook messenger app, with a tab bar controller inside the master view. Seeenter image description here

Ive done exactly as described in this answer Create a TabBar Controller with a Master-detail template?

However! It does not work correctly on iPhone, only on iPad. On iPhone, the navigation backwards does not work. The detail pane opens up just like a modal seque with no possibility of moving backwards. What could be the error here? Is this even possible to achieve with standard uisplitviewcontroller? I have tried embedding navigationcontroller in tabbarcontroller also (making navigationcontroller as root in master view), then it works for iPhone but not iPad.

Persson answered 12/11, 2015 at 10:11 Comment(3)
You might find some info on this issue here: <#31330052>Pomeroy
@Persson how did you end up solving this?Cardew
@Cardew Couldnt solve it, redesigned my flow. If you find a solution that works both for iPad and iPhone please post it and I will accept it. ThanksPersson
A
5

I ended up getting around this by not using a UITabBarController, instead creating a CustomTabBarController which inherits from UIViewController. The custom controller has a UITabBar at the bottom of its view, and multiple other UIViewControllers embedded in Container Views. The custom controller sets the isHidden property to true for of all of the embedded view controller except the one which corresponds to the selected tab.

The following is a simple example with two tabs, identified by their tag:

class CustomTabBarController: UIViewController, UITabBarDelegate {

    @IBOutlet weak var tab1View: UIView!
    @IBOutlet weak var tab2View: UIView!
    @IBOutlet weak var tabBar: UITabBar!

    override func viewDidLoad() {
        tabBar.delegate = self
    }

    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        tab1View.isHidden = item.tag != 1
        tab2View.isHidden = item.tag != 2
    }
}

This custom controller should be set to the root of a UINavigationController, which itself should be set as the master controller of the Split View Controller:

Storyboard showing custom tab bar controller in a split view controller

This setup works for both iPad and iPhone:

iPhone screenshot showing split view in combined mode            iPad screenshot showing split view in split mode

There are a few drawbacks to this method:

  • The custom tab controller is less easy to work with - adding a new tab requires that you add another embedded view and connect it to an outlet in the controller.

  • Setting the navigation item's title and left and right bar button items must be done within the custom tab bar controller, upon tab selection.

  • This method uses (I think) more memory than a regular UITabBarController, since all of the child view controllers are loaded as soon as the app loads, rather than when they are first shown.

  • This setup will lead to the tab bar being hidden when detail is shown in (portrait) iPhone mode. This was what I wanted, and is the behaviour in the Facebook Messenger app too, but if you want the tab bar to be permanently visible, this method won't do it.

Athene answered 25/10, 2016 at 19:53 Comment(1)
Correct if i'm wrong but viewDidLoad gets called on every tab connected to a UITabBarController in a traditional setup. So I don't believe the memory hit in your solution is that different than the first party tab controller provided by apple.Crossjack

© 2022 - 2024 — McMap. All rights reserved.