UISplitViewController in portrait on iPhone always

2019-03-16 17:30发布

问题:

UISplitViewController in portrait on iPhone always show master and detail in iOS 8

I try to subclass UISplitViewController and config it to show master and detail at the same time. but no any effect.

class APPSplitViewController: UISplitViewController, UISplitViewControllerDelegate {

    override func viewDidLoad() {

        super.viewDidLoad()
        preferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible
        maximumPrimaryColumnWidth = 32.0
        minimumPrimaryColumnWidth = 32.0
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

How to?

回答1:

UISplitViewController use show side-by-side only in horizontally regular environment (new TraitCollection size)

The split view controller determines the arrangement of its child view controllers based on the available space. In a horizontally regular environment, the split view controller presents its view controllers side-by-side whenever possible. In a horizontally compact environment, the split view controller acts more like a navigation controller, displaying the primary view controller initially and pushing or popping the secondary view controller as needed. You can also ask the split view controller to prefer a specific arrangement by assigning a value to the preferredDisplayMode property.

Solution. You have to change TraitCollection of SplitViewController. How do this:

  • Create a ViewController and add your SplitViewController as a child.
  • Override traitCollection with size .Regular for your child (UISplitViewController)

ViewController Wrapper

class TraitOverrideViewController: UIViewController {

 override func viewDidLoad() {
    super.viewDidLoad()
    configureSplitVC()
  }


  private func configureSplitVC() {

    let splitVC = self.childViewControllers[0] as UISplitViewController
    setOverrideTraitCollection(UITraitCollection(horizontalSizeClass: .Regular), forChildViewController: splitVC)
  }
}

In iOS 8 UISplitViewController uses Adaptive User Interfaces and TraitCollections to show its content.
It shows different style depending on the size of and type of the view. You can change by technic I explain above.
You can get more info in WWDC video about Building Adaptive Apps with UIKit