【问题标题】:Swift layout per device每个设备的 Swift 布局
【发布时间】:2020-10-02 08:25:21
【问题描述】:

为每个设备创建布局的最佳方式是什么?

现在我正在处理约束,但我无法正确处理。 我所谓的 gameView 的宽度/高度太小,不适合拼图。

这是适合 iPhone 11 拼图的正确宽度/高度

这是 iPhone 8 上的视图

碎片

我的解决方案是设置每个设备的帧大小 但我不知道这是否是正确的解决方案.. 我可以想象有更好的解决方案..

      if UIDevice.current.deviceCategory() == .iPhone8 {
        turnOffConstraints()

        topView.frame = CGRect(x: 0, y: 0, width: topView.frame.width, height: 70)
        categoryLabel.frame = CGRect(x: 88, y: 23, width: 199, height: 35)
        totalMovesSV.frame = CGRect(x: 308, y: 78, width: 50.5, height: 44)
        timerSV.frame = CGRect(x: 20, y: 78, width: 50.5, height: 44)

        gameView.frame = CGRect(x: 20, y: 130, width: 335, height: 335)
        hintSV.frame = CGRect(x: 161, y: 473, width: 52, height: 80.5)

        backButtonSV.frame = CGRect(x: 52, y: 8, width: 52, height: 80.5)
        showOriginalButtonSV.frame = CGRect(x: 161, y: 8, width: 52, height: 80.5)
        levelsButtonSV.frame = CGRect(x: 275, y: 8, width: 52, height: 80.5)

        bottomView.frame = CGRect(x: 0, y: 567, width: topView.frame.width, height: 100)
    }

该应用程序正在运行,一切都符合我的要求,但现在我的调试器充满了这种消息:

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful. 2020-06-12 15:24:55.282095+0200 Sliding Game[35445:2539475] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)

我很好奇正确的方法是什么..

【问题讨论】:

    标签: ios swift layout device


    【解决方案1】:

    您为所有组件提供恒定值。它可能在某些设备上看起来不错,但这并不意味着总体上看起来不错。假设您为组件提供 160px 高度。它在iPhone XsMax(高度为 896 像素)上几乎是屏幕的五分之一。但是当你在iPhone 5, SE..(高度为 568px)中运行时,几乎是屏幕的四分之一。

    赋予恒定值可能会发生在组件之间的交织。这就是 Debug 画面想要表达的意思。

    您可以估计屏幕上的组件或相互锚定以避免它

    【讨论】:

      猜你喜欢
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      相关资源
      最近更新 更多