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

后端 未结 3 1042
既然无缘
既然无缘 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:27

    Unfortunately, the selected answer didn't work for me. However, I did eventually manage to solve the problem:

    1. Subclass UISplitViewController and set the new class in Interface Builder.
    2. Make the new class conform to UISplitViewControllerDelegate:

      required init?(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
          self.delegate = self
      }
      
    3. Implement these two methods:

      func splitViewController(_ splitViewController: UISplitViewController,
                               collapseSecondary secondaryViewController:UIViewController,
                               onto primaryViewController:UIViewController) -> Bool {
          return true
      }
      
      func splitViewController(_ splitViewController: UISplitViewController,
                               showDetail vc: UIViewController,
                               sender: Any?) -> Bool {
      
          if splitViewController.isCollapsed {
              guard let tabBarController = splitViewController.viewControllers.first as? UITabBarController else { return false }
              guard let selectedNavigationViewController = tabBarController.selectedViewController as? UINavigationController else { return false }
      
              // Push view controller
              var detailViewController = vc
              if let navController = vc as? UINavigationController, let topViewController = navController.topViewController {
                  detailViewController = topViewController
              }
              selectedNavigationViewController.pushViewController(detailViewController, animated: true)
              return true
          }
          return false
      }
      

提交回复
热议问题