Adaptive show detail segue transformed to modal instead of push on iPhone when master view controller is a UITabBarController

后端 未结 3 1040
既然无缘
既然无缘 2021-02-07 09:41

In XCode 6, if you create a new project based on the Master-Detail Application template, you get a universal storyboard that is supposed to be good for all devices.

When

3条回答
  •  迷失自我
    2021-02-07 10:10

    Re-watching videos from WWDC14 I think I've found a better answer.

    1. Use a custom UISplitViewController (subclass)
    2. Override the showDetailViewController operation
    3. Use the traitCollection to determine the class of the UISplitViewController
    4. If the horizontal class is Compact, get the navigationController to call showViewController

    Here is the the code of the custom UISplitViewController :

    import UIKit
    
    class CustomSplitViewController: UISplitViewController {
    
        override func showDetailViewController(vc: UIViewController!, sender: AnyObject!) {
    
            if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClass.Compact) {
                if let tabBarController = self.viewControllers[0] as? UITabBarController {
                    if let navigationController = tabBarController.selectedViewController as? UINavigationController {
                        navigationController.showViewController(vc, sender: sender)
                        return
                    }
                }
            }
    
            super.showDetailViewController(vc, sender: sender)
        }
    }
    

    Do not forget to the set the custom class in the storyboard.

    Tested in the simulator of iPhone 6, iPhone 6+ and iPad Air and worked as expected.

提交回复
热议问题