How to call different Storyboards via Swift for iOS?
19,701
I guess you want to open a view? If so this code will do the job:
var mainView: UIStoryboard!
mainView = UIStoryboard(name: "vcLogin", bundle: nil)
let viewcontroller : UIViewController = mainView.instantiateViewControllerWithIdentifier("iPhone5") as UIViewController
self.window!.rootViewController = viewcontroller
It will open the view controller with id: yourViewControllerId
You need to give your viewcontroller an identifier. You do that by highlighting your view controller and then give it a identifier: You then put your identifier in StoryBoard ID.
So for you it will be:
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
}
Author by
Foddy
Updated on July 18, 2022Comments
-
Foddy almost 2 years
I created an app with three different Storyboards for each iOS device family. Now I don't know how to choose the right Storyboard when the app starts? I am checking the screen height to recognize the different devices:
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 }
Can somebody give me a working solution in Swift? I only found solutions for ObjC.
Thanks.
-
Foddy almost 10 yearsHi! Thanks for your answer ;) But that was not really what I searched for. I created 3 different Storybaord files. Now I want to load a completely different Storyboard file when the app starts and based on the screen height...?! I tried your solution but I get some erros: i.minus.com/jKYZ16oT4aUTd.png
-
Foddy almost 10 yearsDoes it mean (with your solution) that I need to put all my Views etc. in one Storyboard (from the iPad and iPhone...) and that call the first View based on its id?
-
Bas almost 10 yearsYes! I thought you had 1 storyboard with 3 different ViewControllers in it.
-
Foddy almost 10 yearsThis is what I am searching for but in Swift: link
-
gutte about 7 yearsDon't forget to dismiss the previous rootViewController