My app is suitable for iPad and iPhone. Due to the size difference, the layout between the devices is completely different and I opted for two different storyboards.
Everything works in the simulators and on real devices, except for my own iPhone SE (2nd gen), but it does work on my wife’s iPhone SE (2nd gen). At first, I thought it was only my device, but this week one of my biggest customers turned out to have the same problem. He uses an iPhone 11.
In my SceneDelegate I check which device is used to select the correct storyboard using the following code:
var storyboard: UIStoryboard {
switch UIDevice.current.userInterfaceIdiom {
case .phone:
print("This is an iphone")
return UIStoryboard(name: "Main-iPhone", bundle: nil)
case .pad:
print("This is an ipad")
return UIStoryboard(name: "Main-iPad", bundle: nil)
default:
print("This is default")
return UIStoryboard(name: "Main", bundle: nil)
}
}
window?.rootViewController = storyboard.instantiateInitialViewController()
window?.makeKeyAndVisible()
guard let _ = (scene as? UIWindowScene) else { return }
I check the choice through the print statement. Everything is still going well here. I use the same switch statement in my ViewController to trigger a different behavior when it comes to an iPhone. The app crashes when connecting an element from the UI that is not known.
It turns out that the storyboard for the iPad is loaded instead of that of the iPhone. Even if I give each storyboard its ViewController, the app on my device continues to load the wrong storyboard.
I have deleted the app from my device several times. Turned the device on and off. Storyboards were removed and added again. A build folder clean. Removing derived data. But nothing helps.
Does anyone recognize this problem?
Many thanks
2
Answers
Which interface is your Main Interface set in deployment info config ? If the problem happens only on phones you can try setting
Main-iPhone
as your Main Interface and only override it for iPad inSceneDelegate
First, make sure you’ve removed any references to Main Interface and Storyboard Name in
info.plist
.Using your code as-is, I don’t see the
print()
statements…Try changing it to this: