8

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
  • 467
  • 1
  • 6
  • 15

1 Answers1

15

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. enter image description here

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
}
Bas
  • 4,423
  • 8
  • 36
  • 53
  • Hi! 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: http://i.minus.com/jKYZ16oT4aUTd.png – Foddy Jul 19 '14 at 13:12
  • Does 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? – Foddy Jul 19 '14 at 13:17
  • Yes! I thought you had 1 storyboard with 3 different ViewControllers in it. – Bas Jul 19 '14 at 13:18
  • This is what I am searching for but in Swift: [link](http://stackoverflow.com/questions/12536689/xcode-4-5-how-to-pick-storyboards-at-launch) – Foddy Jul 19 '14 at 13:19
  • 1
    Don't forget to dismiss the previous rootViewController – gutte Mar 28 '17 at 19:39