The not-so-obvious UISplitViewController warning

Ilter Cengiz
Objective Tidbits
Published in
1 min readApr 5, 2018

If you’re using UISplitViewController in your app and mostly (if not completely) building your view controller hierarchy in code instead of adding view controllers to it in storyboard, you may have seen this warning:

[Warning] Splitview controller <UISplitViewControllerClassicImpl: 0x7fd5fae07c00> is expected to have a view controller at index 0 before it's used!

At first I didn’t pay attention to this warning as my app was already working as I expected. But then, as always, it just annoyed me. After a quick research what would be causing this I came across to this StackOverflow answer.

So in my case I was doing the following:

splitViewController.preferredDisplayMode = .allVisible
splitViewController.viewControllers = [...]

Probably, setting the preferredDisplayMode is triggering the view to be loaded and since we don’t have any view controllers assigned, it’s just printing the above warning to the console.

The solution is simple. Just assign the view controllers first and later do whatever you need to do with split view controller. 😁

splitViewController.viewControllers = [...]
splitViewController.preferredDisplayMode = .allVisible
// any further customizations

--

--

Ilter Cengiz
Objective Tidbits

iOS developer at Careem. Writes at @objtidbits. Hobbyist photographer. Pokémon master, Nintendo addict.