Async- Async- - 1 year ago 90
iOS Question

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?


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:


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

Answer Source

Solution: in the SplitViewController add a call for setNeedsLayout():

override func viewDidAppear(animated: Bool) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download