Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create UISplitViewController programmatically in Swift

Is there anyone who can help me to explain how to make UISpliterController programmatically in Swift. In my application I want to apply the supporting feature of iphone device and ipad. If the app is running on iphone then use single controler but if the application is running on ipad then use UISpliterController with existing ViewController.

I have tried it but it always produce blackscreen Here is my code.

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
}
like image 226
Dodi Avatar asked Dec 23 '22 17:12

Dodi


1 Answers

if you want do it with navigationController, then try it:

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
    }
}
like image 95
Bhautik Ziniya Avatar answered Mar 04 '23 09:03

Bhautik Ziniya