作者:泛傲海 | 来源:互联网 | 2022-12-26 19:39
1> Connor..:
该代码看起来正确。如果您担心到处都有重复项,则有多种方法可以合并它。您可以执行以下操作:
extension UIViewController {
var correctLayoutGuide: UILayoutGuide {
if #available(iOS 11.0, *) {
return view.safeAreaLayoutGuide
}
else {
return view.layoutMarginsGuide
}
}
}
然后您的代码片段可能就是:
NSLayoutConstraint.activate([
theImage.heightAnchor.constraint(equalTo: correctLayoutGuide.heightAnchor, multiplier: 0.5)
theImage.bottomAnchor.constraint(equalTo: correctLayoutGuide.bottomAnchor, constant: -20),
theImage.trailingAnchor.constraint(equalTo: correctLayoutGuide.trailingAnchor, constant: 20)
])