UISplitViewController用于把屏幕分割为左右两部分,一般左边显示导航视图,在右边显示相应的详细信息。从iOS8开始在iPhone上也可以使用了。
一般UISplitViewController都被用在应用的根视图,我们在 storyboard 上拖拽一个Split View Controller会是下图这个样子
我们这个基础上再加上,勾选 Is Initial Controller选项,还有给子视图添加一个UINavigationController
最后我们只需要在AppDelegate 的
funcapplication(application:UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) ->Bool 方法
添加如下代码
letsplitViewController =self.window!.rootViewControlleras!UISplitViewController
letnavigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1]as!UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem= splitViewController.displayModeButtonItem()
splitViewController.delegate=self
注意:相应的代理方法还是要添加的 UISplitViewControllerDelegate
funcsplitViewController(splitViewController:UISplitViewController, collapseSecondaryViewController secondaryViewController:UIViewController, ontoPrimaryViewController primaryViewController:UIViewController) ->Bool{
guardletsecondaryAsNavController = secondaryViewControlleras?UINavigationControllerelse{returnfalse}
guardlettopAsDetailController = secondaryAsNavController.topViewControlleras?DetailViewControllerelse{returnfalse}
iftopAsDetailController.detailItem==nil{
// Return true to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
returntrue
}
returnfalse
}