Multiple storyboards in Swift

I have an application in which there are 4 storyboards, each for a different device (iphone4, 5, 6, 6+). I tried to use automatic restrictions, but since my application is complicated, I could not figure it out. I heard that if you have multiple storyboards, there should be a way to indicate which storyboard to use in the application delegate. How can I specify the correct storyboard for the correct device in the application delegate? I currently have four storyboards called:

iphone4s.storyboard
iphone5.storyboard
iphone6.storyboard
iphone6plus.storyboard

Thanks.

+6
source share
3 answers

Implement a function that retrieves the storyboard when specifying a name.

func getStoryBoard(name : String)-> UIStoryboard { var storyBoard = UIStoryboard(name: name, bundle: nil); return storyBoard; } 

Change the delegation method of the application, for example:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { var storyBoard = getStoryBoard("Main") // Change the storyboard name based on the device, you may need to write a condition here for that var initialViewController: UIViewController = storyBoard.instantiateInitialViewController() as! UIViewController self.window?.rootViewController = initialViewController self.window?.makeKeyAndVisible() // Override point for customization after application launch. return true } 
+1
source

According to my words, today we use autoLayout , which knows how to expand its presentation depending on the screen size and limitations

As you requested, you can override the storyboard from AppDelegate in the applicationdidFinishLaunchingWithOptions method

  let storyBoard : UIStoryboard = UIStoryboard(name: "iphone4s", bundle: nil) //you can check your device and override the storyboard name let initialViewController: UIViewController = storyBoard.instantiateInitialViewController()! self.window?.rootViewController? = initialViewController 
+3
source

To run different storyboards for different devices, you need to redefine the rootViewController object.

  var myDeviceStoryboard: UIStoryboard = UIStoryboard (name: "DeviceStoryboard", bundle: NSBundle.mainBundle ()) 
var firstVC: MyDeviceFirstViewController = myDeviceStoryboard.instantiateViewControllerWithIdentifier ("MyDeviceFirstViewController") as! MyDeviceFirstViewController
self.window? .rootViewController = MyDeviceFirstViewController // This should be a navigation controller

In addition, you need to override several settings in the project and storyboard settings. Refer to the attached images. enter image description here

+1
source

All Articles