[英]"Unable to simultaneously satisfy constraints" with UITableViewCell
我使用 Interface Builder 在UITableViewCell
为我的 Xcode 13.0/Swift 5/iOS 13.5 应用程序创建了一个非常简单的布局:
UIStackView
(里面有 2 个UIView
和UILabel
)在顶部,一个UIView
(里面有一个UIImageView
)在它下面。
目前的确切限制是:
UIStackView
: 10pts 领先,10pts 尾随,0pts 顶部,10pts 到底部的UIView
,50pts 的高度UIView
: 20pts 前导,20pts 尾随,10pts 底部,(从 UIStackView 顶部UIStackView
),高度=宽度UIView
内的UIImageView
: 0pts 前导/尾随/顶部/底部,将中心 x 对齐到 Superview (= UIView
) 与关于 stackoverflow 上警告的其他问题相反,我不直接设置UITableViewCell
的高度,而是希望它由其内容设置。 UIImageView
的图像在运行时设置并应保持其形状,因此方形图像应为方形,其他矩形图像应水平居中(-> 左/右不可见条,如有必要)但不是垂直居中。
Xcode 不会抱怨缺少约束,但这种布局一直给我臭名昭著的“无法同时满足约束”警告。
[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:0x600002d22d50 UIStackView:0x7fe4fa293330.height == 50 (active)>",
"<NSLayoutConstraint:0x600002d22df0 UIView:0x7fe4fa29d1c0.height == UIView:0x7fe4fa29d1c0.width (active)>",
"<NSLayoutConstraint:0x600002d23070 V:|-(0)-[UIStackView:0x7fe4fa293330] (active, names: '|':UITableViewCellContentView:0x7fe4fa2840f0 )>",
"<NSLayoutConstraint:0x600002d23110 H:[UIView:0x7fe4fa29d1c0]-(20)-| (active, names: '|':UITableViewCellContentView:0x7fe4fa2840f0 )>",
"<NSLayoutConstraint:0x600002d23160 V:[UIStackView:0x7fe4fa293330]-(10)-[UIView:0x7fe4fa29d1c0] (active)>",
"<NSLayoutConstraint:0x600002d231b0 H:|-(20)-[UIView:0x7fe4fa29d1c0] (active, names: '|':UITableViewCellContentView:0x7fe4fa2840f0 )>",
"<NSLayoutConstraint:0x600002d23200 V:[UIView:0x7fe4fa29d1c0]-(10)-| (active, names: '|':UITableViewCellContentView:0x7fe4fa2840f0 )>",
"<NSLayoutConstraint:0x600002d36530 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fe4fa2840f0.height == 325.5 (active)>",
"<NSLayoutConstraint:0x600002d364e0 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7fe4fa2840f0.width == 295 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600002d22df0 UIView:0x7fe4fa29d1c0.height == UIView:0x7fe4fa29d1c0.width (active)>
我已经检查过wtfautolayout.com但我能看到的唯一错误是高度:如果宽度是 295,那么高度应该正好是 50+10+295-40+10=325(UIStackView.height + 约束在-between + UIView.width - 前导/尾随约束 + 底部约束),而不是 325.5。 其他一切对我来说都很好,所以我不确定为什么它会计算出错误的高度(如果这就是问题的话)。
我已经尝试了几件事:
UIStackView
的固定高度(在一切看起来都很好之前没有使用它并且它没有抱怨它,但仍然收到警告)UIView.width = UIImageView.height
(1:1 纵横比,而不是上面的那个)UIImageView.width = UIImageView.height
(1:1 纵横比,而不是上面的那个)没有任何效果,我不断收到约束警告,如果由于我更改了某些内容而没有在纵向模式下显示,那么一旦我切换到横向模式,它就会出现。 如何在 IB/无代码中解决此问题?
尝试将 height=width 约束的优先级更改为High而不是Required
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.