我决定避免使用自动布局,因此我目前正在尝试实现代码,以使我的应用程序根据屏幕尺寸管理两个不同的故事板。
我一直在关注这个教程: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()
编辑:我已经删除了as UIViewController
因为不再需要它了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)