Search code examples
iosswiftipadswift2uisplitviewcontroller

Why can I not place Master and Detail view next to each other in UISplitViewController on the first run, but upon rotation it works?


I have a split view controller that has a list of items on the left and a detail view on the right. Relevant code in AppDelegate:

let splitViewController = mainView.instantiateViewControllerWithIdentifier("initial") as! UISplitViewController



        let rightNavController = splitViewController.viewControllers.last as! UINavigationController
        let detailViewController = rightNavController.topViewController as! DetailsIpad

        let leftNavController = splitViewController.viewControllers.first as! UINavigationController
        let masterViewController = leftNavController.topViewController as! MainViewController

        masterSplitViewController = masterViewController
        detailSplitViewController = detailViewController

        // Override point for customization after application launch.
        let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
        navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
        splitViewController.delegate = self

        self.window!.rootViewController = splitViewController

When I first launch the app I see that the right part of the split screen takes up all of the screen:

enter image description here

If I rotate the screen, it becomes properly set (probably because both views are present on the screen):

enter image description here

When I set breakpoints everywhere, I see that the detail view on the right gets loaded before the master view on the left (list of items), despite not being called directly. I cannot change the order in which the views of the split screen are called. How can I fix this?

UPDATE:

I am able to set this before showing split view controller:

splitViewController.preferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible

And in the ViewDidLoad of the split controller when I am printing it:

print(self.preferredDisplayMode.rawValue)

I get: 2, which is AllVisible. But still result is the same.


Solution

  • Thanks @Tarun Tyagi for the explanation, but in my case the suggested actions did not work. However, this worked for me: in the SplitViewController I added a call for setNeedsLayout():

    override func viewDidAppear(animated: Bool) {
        self.view.setNeedsLayout()
    }
    

    That will make the view refresh when the view is on screen already.