我一直试图站起来,用纯代码构建自适应布局(不使用故事板)上运行。我用布局锚设置的限制和利用traitCollectionDidChange方法各组约束和其他接口的变化之间变化。我使用switch语句用相应的一组约束来调用相应的方法。大小的改变类
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
switch (traitCollection.horizontalSizeClass, traitCollection.verticalSizeClass) {
case (.regular, .regular):
setupRegularRegular()
case (.compact, .compact):
setupCompactCompact()
case (.regular, .compact):
setupRegularCompact()
case (.compact, .regular):
setupCompactRegular()
default: break
}
}
为了测试,我只改变了一个约束,它是centerYAnchor.constraint。在人像定为-150,在景观我希望它被改为50为iPad,我设置了固定为0
bigLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -150).isActive = true
iPhone和iPad之间切换时,它工作正常。但是,如果你开始从纵向到横向旋转iPhone,布局系统出现故障时,曰:
[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.
(
"<NSLayoutConstraint:0x600000097890 UILabel:0x7fac02c08aa0'Main Label'.centerY == UIView:0x7fac02e0a950.centerY - 150 (active)>",
"<NSLayoutConstraint:0x604000097840 UILabel:0x7fac02c08aa0'Main Label'.centerY == UIView:0x7fac02e0a950.centerY + 50 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x604000097840 UILabel:0x7fac02c08aa0'Main Label'.centerY == UIView:0x7fac02e0a950.centerY + 50 (active)>
我试图重写各种方法和设置约束为false,然后的isActive属性回到真实的,但没有帮助。我在网上搜索了几天。目前还没有解决方案。所以我的问题是,如何在旋转设备时正确切换尺寸类别?是否有必要重写其他方法或其他?纯代码中的自适应布局是否有更好的解决方案?在代码中使用自适应布局/尺寸类别/布局锚点和约束时(不使用故事板),是否有最佳实践? 感谢您的帮助!
P.S.该方法建立约束:
func setupCompactRegular() {
bigLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -150).isActive = true
}
func setupRegularCompact() {
bigLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 50).isActive = true
}
显示您的设置方法的代码。你实际上是在编辑现有的约束还是只创建一个额外的约束? – dan
刚用方法更新了帖子。 – Andrei