Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a UISplitViewController in Swift

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/

like image 365
A.Roe Avatar asked Mar 27 '16 19:03

A.Roe


2 Answers

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
like image 149
RileyDev Avatar answered Oct 18 '22 22:10

RileyDev


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

like image 21
shinoys222 Avatar answered Oct 18 '22 23:10

shinoys222