swift – 在App Delegate中管理两个故事板
发布时间:2020-12-14 04:25:16 所属栏目:百科 来源:网络整理
导读:我决定避免使用自动布局,所以我正在尝试实现代码,以使我的应用程序根据屏幕大小管理两个不同的故事板. 我一直在关注本教程:http://pinkstone.co.uk/how-to-load-a-different-storyboard-depending-on-screen-size-in-ios/ 我在尝试将Objective C代码转换为S
我决定避免使用自动布局,所以我正在尝试实现代码,以使我的应用程序根据屏幕大小管理两个不同的故事板.
我一直在关注本教程: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,因为它不再需要. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |