I have decided to avoid using auto-layout so I am instead currently trying to implement the code to make my app manage two different storyboards based on screen size.
I have been following this tutorial: http://pinkstone.co.uk/how-to-load-a-different-storyboard-depending-on-screen-size-in-ios/
I am having issues trying to translate the Objective C code into Swift.
Here is the code that I currently have in my AppDelegate:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func grabStoryboard() -> UIStoryboard {
var storyboard = UIStoryboard()
var height = UIScreen.mainScreen().bounds.size.height
if height == 480 {
storyboard = UIStoryboard(name: "Main3.5", bundle: nil)
} else {
storyboard = UIStoryboard(name: "Main", bundle: nil)
}
return storyboard
}
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
var storyboard: UIStoryboard = self.grabStoryboard()
self.window?.rootViewController.storyboard.instantiateInitialViewController()
self.window?.makeKeyAndVisible()
return true
}
The app runs, and I have no errors however no matter whether I run the app on a 3.5 inch device or a 4 inch device, I just get the 4 inch storyboard.
Where am I going wrong?
The problem is in line:
self.window?.rootViewController.storyboard.instantiateInitialViewController()
You should use this instead:
self.window?.rootViewController = storyboard.instantiateInitialViewController()
Edit: I have removed as UIViewController
because it is no longer needed.
For anyone using Xcode 7 or above, probably due to Swift 2.0, this line:
self.window?.rootViewController.storyboard.instantiateInitialViewController()
Should actually be:
self.window?.rootViewController = storyboard.instantiateInitialViewController()
Note that as UIViewController
is no longer needed.
来源:https://stackoverflow.com/questions/25465996/managing-two-storyboards-in-app-delegate