【发布时间】:2018-04-21 06:04:25
【问题描述】:
当我设置由 Size Classes 自定义的 Auto Layout 约束时,例如约束安装在某些 Size Class(es) 中,而不是安装在其他 Size Class 中时,我在运行时收到此 Unsatisfiable Constraints 错误消息,但结果符合预期在没有错误/警告的界面生成器上。
我在测试什么
对于这个简化的示例,为了演示 Unsatisfiable Constraints 是如何意外发生的,我希望星形图像具有固定的高度,但基于 Size Class 的自定义宽度。正如您在图片中看到的,我希望常规宽度相当大。
由于 Compact Width 没有安装宽度约束,它会根据星形图像的内在内容获取图像大小。 (来源:这颗星来自Use Your Loaf)
问题
当我在运行 iPhone 8 Plus(iOS 11.1 和 Xcode 9.1)的模拟器中将设备从横向旋转到纵向时,我收到以下错误:
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.
....
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6040002839d0 UIImageView:0x7fe177d0b940.width == 480 (active)>
很明显,即使我的视图更改为紧凑宽度(我指定未安装约束),此宽度约束仍然存在。为什么是这样?使用installed 是根据不同的大小类自定义自动布局约束的正确方法吗?不过,我看不到任何其他方法可以在 Storyboard 中实现它。
我尝试以编程方式创建宽度约束并改为从traitCollectionDidChange 内部设置.isActive,但我仍然遇到同样的错误。
我在测试来自Mysteries of Auto Layout WWDC talk 的示例代码时没有收到此类错误,就像我一样,它通过traitCollectionDidChange 停用/激活约束。
更新
我在GitHub 上添加了自己的代码。
【问题讨论】:
标签: ios swift xcode autolayout size-classes