Managing Two Story Files in App Delegate
I've decided to avoid using auto-layout, so I'm trying to implement code instead so that my app is managing two different storyboards based on screen size.
I am following this tutorial: http://pinkstone.co.uk/how-to-load-a-different-storyboard-depending-on-screen-size-in-ios/
I am having trouble trying to translate Objective C code into Swift.
Here is the code I have in my AppDelegate:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func grabStoryboard() -> UIStoryboard {
var storyboard = UIStoryboard()
var height = UIScreen.mainScreen().bounds.size.height
if height == 480 {
storyboard = UIStoryboard(name: "Main3.5", bundle: nil)
} else {
storyboard = UIStoryboard(name: "Main", bundle: nil)
}
return storyboard
}
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
var storyboard: UIStoryboard = self.grabStoryboard()
self.window?.rootViewController.storyboard.instantiateInitialViewController()
self.window?.makeKeyAndVisible()
return true
}
The app launches and I have no errors, however whether I run the app on a 3.5 "device or a 4" device, I just get a 4 "storyboard.
Where am I going wrong?
source to share
The problem is on the line:
self.window?.rootViewController.storyboard.instantiateInitialViewController()
You should use this instead:
self.window?.rootViewController = storyboard.instantiateInitialViewController()
Edit: I removed as UIViewController
it because it is no longer needed.
source to share
For those using Xcode 7 or higher, perhaps due to Swift 2.0, this line:
self.window?.rootViewController.storyboard.instantiateInitialViewController()
In fact, it should be:
self.window?.rootViewController = storyboard.instantiateInitialViewController()
Please note that is as UIViewController
no longer required.
source to share