我决定避免使用自动布局,所以我正在尝试实现代码,以使我的应用程序根据屏幕大小管理两个不同的故事板. 我一直在关注本教程:http://pinkstone.co.uk/how-to-load-a-different-storyboard-depending-
我一直在关注本教程: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,因为它不再需要.