【问题标题】:Programmatically setting explicit layout constraints constants for explicitly declared screen sizes (logical dimensions)以编程方式为显式声明的屏幕尺寸(逻辑尺寸)设置显式布局约束常量
【发布时间】:2023-04-02 23:02:01
【问题描述】:

我正在努力了解自动布局和不同的显式屏幕尺寸的工作原理。

我只是希望这些显式不同,我将为每个受支持的设备设置.constant,因为尝试使用布局编辑器、过时的 Pod 解决方案、Variable-for-traits 和自动布局来做到这一点是一个偶然-对我来说是一团糟,哈哈。我不介意手工操作,它应该可以在以后的更新/修改中非常容易地进行修改。

这里是我目前拥有的和我认为会是答案但不是答案的 sn-p。当我为 iPhone 12 Pro MaxiPhone 12 mini 运行模拟器时,对象上的尾随约束是相同的,并显示为 0


import UIKit

class ViewController: UIViewController, UITextViewDelegate {
    
    @IBOutlet weak var trailPadSet: NSLayoutConstraint!
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        displayBackGround.layer.cornerRadius = 10
        
        if UIScreen.main.bounds.height == 780 { // <- this is iPhone Mini
            trailPadSet.constant = 120 // <- I set this to a wild constant to just see the difference noticeably.
        } else {
            trailPadSet.constant = 0
        }
    }
}

【问题讨论】:

  • 调试! trailPadSet.constant = 120 行真的执行了吗?
  • @matt 它没有。我收到错误消息 - screen parameters are unexpected: MGScreenClass1125x2436x3x495 SCREEN_TYPE(1125,2436,3,495) 我读到 xcode 12 和 iPhone 12 Mini 中可能存在错误。
  • 干得好!嗯,这就是你的答案。它不会更改为 120,因为您从未将其更改为 120。完美。
  • 没有错误;为什么你认为 iPhone 12 mini 的垂直分辨率是 780?您已经发现了您的方法可能不是最佳主意的原因;您将不得不对许多设备细节进行硬编码,并且每次 Apple 发布新设备时都需要调整您的应用程序。
  • @matt 我主要是想弄清楚为什么大声笑

标签: ios swift xcode autolayout


【解决方案1】:

我认为您正在检查 == 以检查设备。您可以做一件事检查设备名称,如下所示 UIdevice.current.name 它将打印设备名称并且您的代码将起作用

【讨论】:

  • 如果我将我的设备命名为“Sally”会怎样?在名称中包含型号是 Apple 在您设置设备时使用的默认设置,但这不是强制性的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-27
  • 1970-01-01
  • 1970-01-01
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多