Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call different Storyboards via Swift for iOS?

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.

like image 600
Foddy Avatar asked Jul 19 '14 12:07

Foddy


1 Answers

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
}
like image 121
Bas Avatar answered Nov 17 '22 07:11

Bas