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

后端 未结 1 848
广开言路
广开言路 2021-02-06 16:02

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

I try to subclass UISplitViewCont

1条回答
  •  温柔的废话
    2021-02-06 16:42

    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

    0 讨论(0)
提交回复
热议问题