Reputation: 65
I just want to load a specific storyboard when a certain iPhone size loads the app. I'm really struggling to get the desired out come using auto layout.
I've done a lot of searching and found code someone shared 4 years ago and tried using it but I got a lot of errors, could someone with more knowledge have a look at the code and see if it needs updating please?
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject:
AnyObject]?) -> Bool {
var bounds: CGRect = UIScreen.mainScreen().bounds
var screenHeight: NSNumber = bounds.size.height
var deviceFamily: String
var mainView: UIStoryboard!
mainView = UIStoryboard(name: "iphone35Storyboard", bundle: nil)
let viewcontroller : UIViewController = mainView.instantiateViewControllerWithIdentifier("iphone35") as UIViewController
self.window!.rootViewController = viewcontroller
if screenHeight == 480 {
deviceFamily = "iPhoneOriginal"
// Load Storyboard with name: iPhone4
var mainView: UIStoryboard!
mainView = UIStoryboard(name: "Main", bundle: nil)
let viewcontroller : UIViewController = mainView.instantiateViewControllerWithIdentifier("iphone4") as UIViewController
self.window!.rootViewController = viewcontroller
} else {
var mainView: UIStoryboard!
mainView = UIStoryboard(name: "IpadStoryboard", bundle: nil)
let viewcontroller : UIViewController = mainView.instantiateViewControllerWithIdentifier("ipad") as UIViewController
self.window!.rootViewController = viewcontroller
if screenHeight == 920 {
deviceFamily = "Pad"
// Load Storyboard with name: ipad
var mainView: UIStoryboard!
mainView = UIStoryboard(name: "IpadStoryboard", bundle: nil)
let viewcontroller : UIViewController = mainView.instantiateViewControllerWithIdentifier("ipad") as UIViewController
self.window!.rootViewController = viewcontroller
}
}
}
Errors experienced -
Instance method 'application(application:didFinishLaunchingWithOptions:)' nearly matches optional requirement 'application(_:didFinishLaunchingWithOptions:)' of protocol 'UIApplicationDelegate'
'instantiateViewControllerWithIdentifier' has been renamed to 'instantiateViewController(withIdentifier:)'
Cannot call value of non-function type 'UIScreen'
Cannot convert value of type 'CGFloat' to specified type 'NSNumber'
Upvotes: 1
Views: 706
Reputation: 2678
Swift is still evolving and with every version of Swift there are many changes in the syntax. All those errors that you are getting are because that 4 years old code was meant for some older versions of swift. In Swift 4, you can use
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let deviceIdiom = UIScreen.main.traitCollection.userInterfaceIdiom
if deviceIdiom == .pad {
let storyboard = UIStoryboard(name: "YourFirstStoryboard", bundle: nil)
if let firstVC = storyboard.instantiateViewController(withIdentifier: "FirstVC_Identifier") as? FirstVC {
self.window?.rootViewController = firstVC
}
} else if deviceIdiom == .phone {
if (UIDevice.current.userInterfaceIdiom == .phone) && (UIScreen.main.bounds.size.height < 568.0) {
/* "< 568" = iphone 4 or less */
/* Similarly you can use other "else if" conditions with.. */
/* "== 568" = iphone 5 and 5c */
/* "== 667" = iphone 6,7 or 8 */
/* "== 736" = iphone 6P,7P or 8 */
/* "== 812" = iphone X, XR or XS */
/* "== 896" = iphone X, XR or XS */
let storyboard = UIStoryboard(name: "YourSecondStoryboard", bundle: nil)
if let secondVC = storyboard.instantiateViewController(withIdentifier: "SecondVC_Identifier") as? SecondVC {
self.window?.rootViewController = secondVC
}
}
}
self.window?.makeKeyAndVisible()
return true
}
Upvotes: 3