我已决定避免使用自动布局,因此我目前正在尝试实现代码,让我的应用根据屏幕尺寸管理两个不同的 Storyboard。
我一直在关注本教程: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 英寸 Storyboard。
我哪里错了?
最佳答案
问题在一行:
self.window?.rootViewController.storyboard.instantiateInitialViewController()
你应该改用这个:
self.window?.rootViewController = storyboard.instantiateInitialViewController()
编辑:我已将 as UIViewController
删除,因为不再需要它。
关于swift - 在 App Delegate 中管理两个 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25465996/