swift – 在App Delegate中管理两个故事板

我决定避免使用自动布局,所以我正在尝试实现代码,以使我的应用程序根据屏幕大小管理两个不同的故事板.

我一直在关注本教程:http://pinkstone.co.uk/how-to-load-a-different-storyboard-depending-on-screen-size-in-ios/

我在尝试将Objective C代码转换为Swift时遇到问题.

这是我目前在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
}

该应用程序运行,我没有错误,但无论我是在3.5英寸设备或4英寸设备上运行应用程序,我只是得到4英寸的故事板.

我哪里错了?

解决方法

问题在于:

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

您应该使用此代替:

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

编辑:我已删除UIViewController,因为它不再需要.

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...