Ios – How to use a UISplitViewController in Swift

iosipadswiftuisplitviewcontrolleruitabbarcontroller

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/

Best Answer

You are so close just do the following.

Keep the split view layout with detail segues and return true for the following method and remove the rest of the code to do with the variable collapseDetailViewController.

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

Put the following in you Master View controller

self.splitViewController!.delegate = self;

self.splitViewController!.preferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible

self.extendedLayoutIncludesOpaqueBars = true  

Add self.extendedLayoutIncludesOpaqueBars = true to your detail view controller as mentioned by the previous answer. That should remove the bar appearing on your view controllers.

Also if you want some extra functionality add the following if you want your detail view to use the full screen on iPad.

navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem()
navigationItem.leftItemsSupplementBackButton = true
Related Topic