在加载时针对不同的屏幕尺寸使用不同的故事板? xcode Swift

Using different storyboards for different screen sizes on load? xcode Swift

我只想在某个 iPhone 大小加载应用程序时加载特定的故事板。 我真的很难使用自动布局来获得所需的结果。

我做了很多搜索,找到了 4 年前有人分享的代码,并尝试使用它,但我遇到了很多错误,有更多知识的人可以看看代码,看看它是否需要更新请问?

    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
    }
    }
    }

遇到错误 -

实例方法 'application(application:didFinishLaunchingWithOptions:)' 几乎符合协议 'UIApplicationDelegate'

的可选要求 'application(_:didFinishLaunchingWithOptions:)'

'instantiateViewControllerWithIdentifier' 已重命名为 'instantiateViewController(withIdentifier:)'

无法调用非函数类型的值'UIScreen'

无法将类型 'CGFloat' 的值转换为指定类型 'NSNumber'

Swift 仍在不断发展,Swift 的每个版本在语法上都有很多变化。你得到的所有这些错误都是因为 4 年前的代码是为 swift 的一些旧版本设计的。在Swift4中,可以使用

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
    }