How to use a UISplitViewController in Swift

A.Roe picture A.Roe · Mar 27, 2016 · Viewed 7.1k times · Source

So I add a UISplitViewController to a project which is embedded with a UITabBarController.

The UISplitViewController has a UINavigationController as a Master & Detail relationship with their own root controller.

The Master UINavigationController rootController has a detail segue to the Detail UINavigationController.

See here:

enter image description here

All pretty simple right ? Now in the TableViewController I do the following;

class TableViewController: TableViewController, UISplitViewControllerDelegate {

var collapseDetailViewController: Bool  = false

override func viewDidLoad() {
    super.viewDidLoad()

    splitViewController?.delegate = self
}


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    collapseDetailViewController = false
}

// MARK: - UISplitViewControllerDelegate

func splitViewController(splitViewController: UISplitViewController, collapseSecondaryViewController secondaryViewController: UIViewController, ontoPrimaryViewController primaryViewController: UIViewController) -> Bool {
    return false
}

I aslo have a UISplitViewController extension and do the following;

extension UISplitViewController: UISplitViewControllerDelegate {

public override func viewDidLoad() {
    self.extendedLayoutIncludesOpaqueBars = true
}  

}

With all this done I get the following problems;

  • When tapping on tableviewcell to segue to detail view the detail view opens within the master left pane when on iPad in landscape instead of the right. I also cannot figure out how to show the Master View as the first view when in Portrait on iPad or on the iPhone. These two problems may or may not be related I am not sure.

    enter image description here

  • On Mobile there is a bottom bar above the tab bar that I cannot figure out how to remove. I had the same problem on the iPad until I added the code in the UISplitViewController extensions viewDidLoad however that did not effect the mobile. See here,

enter image description here

PS: I am not sure if the question is too long, I felt it is best to put everything in context. Also I have been doing lots of research but I cannot find any resources in swift which use a UITabBarController.

I did follow the following tutorial http://nshipster.com/uisplitviewcontroller/

Answer

shinoys222 picture shinoys222 · Mar 27, 2016

As for the splitViewController's master to be visible, you need to add this in splitViewController's ViewDidLoad. Else the master view controller is present as a side menu which you can drag in Portrait mode

self.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;

But, this master detail view will only be visible in iphone 6 plus and ipad only, otherwise ,it will act just like a navigation controller. As For pushing the viewController to navigation controller, you are trying to push a navigation controller to another navigation controller. I don't think it is recommended. Just move the segue from first view controller (where you input text) to second one(color view controller), instead of the second navigation controller. If you are interested to show the details in the right section for ipad and iphone6, and as a new page for other devices, you should not use this way, remove the push segue and use a delegate to pass information that data is changed and refresh UI.

Storyboard screenshot

Also, i don't think you might need a navigationController as the details page, just the colors viewController might be enough, if you are not interested in further navigation from the details page.

For detailed information on the behaviour of split view controller in iphone and iPad, just check https://www.raywenderlich.com/94443/uisplitviewcontroller-tutorial-getting-started

Try adding self.extendedLayoutIncludesOpaqueBars = true to your navigationController's viewDidLoad for the gap issue