我使用此代码根据屏幕加载每个故事板,但唯一有效的是480,而其他所有不在那里只是空白
我该如何解决这个问题 感谢
var window: UIWindow?
func grabStoryboard() -> UIStoryboard {
var storyboard = UIStoryboard()
let height = UIScreen.mainScreen().bounds.size.height
if height == 480 {
storyboard = UIStoryboard(name: "main3.5", bundle: nil)
if height == 568 {
storyboard = UIStoryboard(name: "main4", bundle: nil)
}
if height == 667 {
storyboard = UIStoryboard(name: "main6", bundle: nil)
}
if height == 736 {
storyboard = UIStoryboard(name: "main6plus", bundle: nil)
}
} else {
storyboard = UIStoryboard(name: "Main", bundle: nil)
}
return storyboard
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let storyboard: UIStoryboard = self.grabStoryboard()
self.window?.rootViewController =
storyboard.instantiateInitialViewController()! as UIViewController
self.window?.makeKeyAndVisible()
return true
}
答案 0 :(得分:1)
568,667和736的if在480语句中,所以永远不会到达。希望有所帮助。
答案 1 :(得分:0)
你的语法错了。您需要以正确的方式使用if-else
语句,如下所示。您可以查看Swift文档Swift Control Flow。转到Conditional Statements
部分。
func grabStoryboard() -> UIStoryboard {
var storyboard = UIStoryboard()
let height = UIScreen.mainScreen().bounds.size.height
if height == 480 {
storyboard = UIStoryboard(name: "main3.5", bundle: nil)
} else if height == 568 {
storyboard = UIStoryboard(name: "main4", bundle: nil)
} else if height == 667 {
storyboard = UIStoryboard(name: "main6", bundle: nil)
} else if height == 736 {
storyboard = UIStoryboard(name: "main6plus", bundle: nil)
} else {
storyboard = UIStoryboard(name: "Main", bundle: nil)
}
return storyboard
}