3

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?

user3746428
  • 11,047
  • 20
  • 81
  • 137

2 Answers2

6

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.

Marián Černý
  • 15,096
  • 4
  • 70
  • 83
  • This is what I had initially. However I am getting the error 'Cannot assign to the result of this expression'. – user3746428 Aug 23 '14 at 21:38
  • Strange. This is a copy-paste from my test project and this is the only change I have made to your code and it works for me. Xcode 6 beta 6. – Marián Černý Aug 23 '14 at 21:41
  • I am currently running beta 4. I haven't updated yet due to low SSD space. Do you think that this could be the issue? – user3746428 Aug 23 '14 at 21:42
  • You should update. I have tested it in beta 4 and it does not work. In beta 5 and beta 6 it works. The following is a "fix" for beta 4, but it then crashes the compiler for me: `if let window = window { window.rootViewController = storyboard.instantiateInitialViewController() as UIViewController }` – Marián Černý Aug 23 '14 at 21:49
  • Ah, I see. I didn't even think to try to update. I'll do that now. Thanks for your help! – user3746428 Aug 23 '14 at 21:50
  • The above mentioned fix works in beta 4. I did not clean the build folder (⌥⇧⌘K) after I have tested different Xcode version. That's why the compiler crashed for me. – Marián Černý Aug 23 '14 at 22:02
  • BTW, you should also change `var storyboard = UIStoryboard()` to `var storyboard: UIStoryboard`. – Marián Černý Aug 23 '14 at 22:04
1

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.

Razattax
  • 163
  • 1
  • 1
  • 12