我为每个iOS设备系列创建了一个包含三个不同Storyboard的应用。现在,当应用程序启动时,我不知道如何选择正确的Storyboard?我正在检查屏幕高度以识别不同的设备:
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Check Device Family
var bounds: CGRect = UIScreen.mainScreen().bounds
var screenHeight: NSNumber = bounds.size.height
var deviceFamily: String
if screenHeight == 480 {
deviceFamily = "iPhoneOriginal"
// Load Storyboard with name: iPhone4
} else if screenHeight == 568 {
deviceFamily = "iPhone5Higher"
// Load Storyboard with name: iPhone5
} else {
deviceFamily = "iPad"
// Load Storyboard with name: iPad
}
return true
}
有人可以在Swift中为我提供有效的解决方案吗?我只找到了ObjC的解决方案。
感谢。
答案 0 :(得分:11)
我想你想打开一个视图?如果是这样,这段代码将完成这项工作:
var mainView: UIStoryboard!
mainView = UIStoryboard(name: "vcLogin", bundle: nil)
let viewcontroller : UIViewController = mainView.instantiateViewControllerWithIdentifier("iPhone5") as UIViewController
self.window!.rootViewController = viewcontroller
它将打开ID为yourViewControllerId
您需要为viewcontroller提供标识符。 您可以通过突出显示视图控制器然后为其指定标识符来执行此操作: 然后,将您的标识符放在StoryBoard ID中。
所以对你而言:
if screenHeight == 480 {
deviceFamily = "iPhoneOriginal"
// Load Storyboard with name: iPhone4
var mainView: UIStoryboard!
mainView = UIStoryboard(name: "vcLogin", bundle: nil)
let viewcontroller : UIViewController = mainView.instantiateViewControllerWithIdentifier("iPhone4") as UIViewController
self.window!.rootViewController = viewcontroller
}