繁体   English   中英

Swift 每个器件的布局

[英]Swift layout per device

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

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

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

iPhone 11

这是 iPhone 8 上的视图iPhone 8

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

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

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)

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

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

给出恒定值可能会发生在组件之间的交织。 这就是调试屏幕要说的。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM