Managing two storyboards in App Delegate

随声附和 提交于 2019-12-01 06:02:34

问题


I have decided to avoid using auto-layout so I am instead currently trying to implement the code to make my app manage two different storyboards based on screen size.

I have been following this tutorial: http://pinkstone.co.uk/how-to-load-a-different-storyboard-depending-on-screen-size-in-ios/

I am having issues trying to translate the Objective C code into Swift.

Here is the code that I currently 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 runs, and I have no errors however no matter whether I run the app on a 3.5 inch device or a 4 inch device, I just get the 4 inch storyboard.

Where am I going wrong?


回答1:


The problem is in line:

self.window?.rootViewController.storyboard.instantiateInitialViewController()

You should use this instead:

self.window?.rootViewController = storyboard.instantiateInitialViewController()

Edit: I have removed as UIViewController because it is no longer needed.




回答2:


For anyone using Xcode 7 or above, probably due to Swift 2.0, this line:

self.window?.rootViewController.storyboard.instantiateInitialViewController()

Should actually be:

self.window?.rootViewController = storyboard.instantiateInitialViewController()

Note that as UIViewController is no longer needed.



来源:https://stackoverflow.com/questions/25465996/managing-two-storyboards-in-app-delegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!