【问题标题】:Using different storyboards for different screen sizes on load? xcode Swift在加载时为不同的屏幕尺寸使用不同的故事板?斯威夫特
【发布时间】:2019-08-19 19:40:01
【问题描述】:

我只想在特定 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”类型

【问题讨论】:

  • 不要使用任何代码,扔掉它并学习自动布局。如果您正在为此苦苦挣扎,请发布一个问题。这是一个非常简单的 API,可以很快学会。
  • 谢谢,但如果可能的话,我真的只需要让上面的代码工作。
  • 自动布局和故事板旨在完全按照您的问题要求执行。苹果不希望你按照自己的方式去做。如果他们看到你这样做,没有人会雇用你作为程序员。用正确的方法学习它,非常简单。如果您无法获得自动布局或故事板工作,请在此处发布问题,我们将很乐意回答。但是上面的代码是一个完整的非入门 IMO。

标签: swift storyboard screen-size


【解决方案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
    }

【讨论】:

  • 非常感谢您抽出宝贵时间回复。但是,我仍然收到上述代码的错误 - (使用未声明的类型“FirstVC”)我应该在哪里或如何使用 FirstVC?
  • FirstVC 只是我使用的一个名称(我的意思是您的第一个视图控制器),就像“SecondVC”、“YourFirstStoryboard”和“YourSecondStoryboard”。你可以在那里给你的视图控制器的名字。我明白了,您没有为视图控制器使用任何子类。好的,那你就可以用"let viewcontroller : UIViewController = mainView.instantiateViewControllerWithIdentifier("iphone4") as UIViewController"
  • 谢谢,但我现在不知道在哪里放置新代码 let viewcontroller : UIViewController = mainView.instantiateViewControllerWithIdentifier("iphone4") as UIViewController 当我替换我认为应该的部分时,我仍然会收到错误(使用未解析的标识符“mainView”),非常抱歉我缺乏知识,我非常感谢这种帮助。此外,我不需要将我的设备声明为我只在 iPhone 上运行...如果这有帮助吗?
  • 和一个 - 线程 1:信号 SIGABRT - 在 AppDelegate 类上:UIResponder 行
  • 你可以使用storyboard代替mainView。故事板和 mainView 只是我给出的随机名称。您可以为此使用任何名称
猜你喜欢
  • 1970-01-01
  • 2016-03-07
  • 1970-01-01
  • 2013-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多