使用不同的情节提要板来加载不同的屏幕尺寸? xcode Swift

时间:2019-03-28 22:17:37

标签: swift storyboard screen-size

我只想在某个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(application:didFinishLaunchingWithOptions :)')与协议'UIApplicationDelegate'的可选要求'application(_:didFinishLaunchingWithOptions :)'几乎匹配

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

无法调用非功能类型'UIScreen'的值

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

1 个答案:

答案 0 :(得分:1)

Swift仍在不断发展,每个版本的Swift语法都有很多变化。您得到的所有这些错误是因为那4年的代码原是用于某些较早版本的swift的。在Swift 4中,您可以使用

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
    }