How to use a UISplitViewController in Swift

廉价感情. 提交于 2019-12-04 11:20:37

问题


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:

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.

  • 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,

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/


回答1:


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



回答2:


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.

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



来源:https://stackoverflow.com/questions/36251897/how-to-use-a-uisplitviewcontroller-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!