【发布时间】:2023-04-02 23:02:01
【问题描述】:
我正在努力了解自动布局和不同的显式屏幕尺寸的工作原理。
我只是希望这些显式不同,我将为每个受支持的设备设置.constant,因为尝试使用布局编辑器、过时的 Pod 解决方案、Variable-for-traits 和自动布局来做到这一点是一个偶然-对我来说是一团糟,哈哈。我不介意手工操作,它应该可以在以后的更新/修改中非常容易地进行修改。
这里是我目前拥有的和我认为会是答案但不是答案的 sn-p。当我为 iPhone 12 Pro Max 和 iPhone 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