如何在 Swift 中以编程方式创建 UISplitViewController

How to create UISplitViewController programmatically in Swift

有没有人可以帮助我解释如何在 Swift 中以编程方式制作 UISpliterController。在我的应用程序中,我想应用 iphone 设备和 ipad 的支持功能。如果应用程序是 iphone 上的 运行,则使用单个控制器,但如果应用程序是 ipad 上的 运行,则使用现有 ViewController.[=12= 的 UISpliterController ]

我试过了,总是黑屏 这是我的代码。

if UIDevice.current.userInterfaceIdiom == .pad {

    let spliterVC = UISplitViewController()
    let homeNavControler = mainStoryboard.instantiateViewController(withIdentifier: "homeViewController") as! HomeViewController

    let secondVC = mainStoryboard.instantiateViewController(withIdentifier: "secondViewController") as! SecondViewController
    spliterVC.viewControllers = [homeNavControler,secondVC]
    appdelegate.window?.rootViewController = spliterVC
}

如果你想用 navigationController 做,那就试试吧:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    if UIDevice.current.userInterfaceIdiom == .pad {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window!.backgroundColor = UIColor.whiteColor()
        var splitViewController =  UISplitViewController()
        var homeViewController = HomeViewController()
        var secondViewController = SecondViewController()
        var homeNavigationController = UINavigationController(rootViewController:homeViewController)
        var secondNavigationController = UINavigationController(rootViewController:secondViewController)
        splitViewController.viewControllers = [homeNavigationController,secondNavigationController]
        self.window!.rootViewController = splitViewController
        self.window!.makeKeyAndVisible()
        return true
    } else {
        // use single controller for iPhone and return that controller
    }
}