我认为您应该更通用地解决此问题。您可以子类化UISplitViewController并在嵌入式视图控制器中实现协议。
class MasterShowingSplitViewController: UISplitViewController {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}
}
extension MasterShowingSplitViewController: UISplitViewControllerDelegate {
func splitViewController(splitViewController: UISplitViewController,
collapseSecondaryViewController secondaryViewController: UIViewController,
ontoPrimaryViewController primaryViewController: UIViewController) -> Bool {
guard let masterNavigationController = primaryViewController as? UINavigationController,
master = masterNavigationController.topViewController as? SplitViewControllerCollapseProtocol else {
return true
}
return master.shouldShowMasterOnCollapse()
}
}
protocol SplitViewControllerCollapseProtocol {
func shouldShowMasterOnCollapse() -> Bool
}
UITableViewController中的示例实现:
extension SettingsTableViewController: SplitViewControllerCollapseProtocol {
func shouldShowMasterOnCollapse() -> Bool {
return tableView.indexPathForSelectedRow == nil
}
}
希望能帮助到你。因此,您可以重用此类,只需要实现一个协议即可。
UISplitViewController
并始终YES
从该方法返回,然后只更改了Storyboard中的拆分视图类,因为我一直希望以肖像的方式在iPhone上显示母版。:)