Swift 每个设备的布局

Swift layout per device

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

现在我正在处理一些限制,但我做不对。 我所谓的 gameView 中的 width/height 太小了,装不下拼图。

这是正确的width/height拼图iPhone11

这是iPhone8上的观点

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

      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)

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

您为所有设备提供了恒定值 components.Its 也许在某些设备上看起来不错,但这并不意味着在 all.Lets 中看起来不错,比如您没有为组件提供 160px 高度。它在 iPhone XsMax(高度为 896px)上看起来不错,几乎是屏幕的五分之一。但是当你 运行 in iPhone 5, SE..(height is 568px) 几乎是屏幕的四分之一。

赋予恒定值可能会在组件之间交织在一起。这就是Debug screen想说的

你可以将组件估计到屏幕上或者相互锚定以避免它