简体   繁体   English

“无法同时满足约束”与 UITableViewCell

[英]"Unable to simultaneously satisfy constraints" with UITableViewCell

I used the Interface Builder to create a pretty simple layout inside a UITableViewCell for my Xcode 13.0/Swift 5/iOS 13.5 app:我使用 Interface Builder 在UITableViewCell为我的 Xcode 13.0/Swift 5/iOS 13.5 应用程序创建了一个非常简单的布局:

UIStackView (with 2 UIView s and UILabel s inside) at the top and a UIView (with a UIImageView inside) below it. UIStackView (里面有 2 个UIViewUILabel )在顶部,一个UIView (里面有一个UIImageView )在它下面。

The exact constraints are currently:目前的确切限制是:

  • UIStackView : 10pts leading, 10pts trailing, 0pts top, 10pts to the UIView at the bottom, height of 50pts UIStackView : 10pts 领先,10pts 尾随,0pts 顶部,10pts 到底部的UIView ,50pts 的高度
  • UIView : 20pts leading, 20pts trailing, 10pts bottom, (10pts top from the UIStackView ), height=width UIView : 20pts 前导,20pts 尾随,10pts 底部,(从 UIStackView 顶部UIStackView ),高度=宽度
  • UIImageView inside the UIView : 0pts leading/trailing/top/bottom, align center x to Superview (= UIView ) UIView内的UIImageView : 0pts 前导/尾随/顶部/底部,将中心 x 对齐到 Superview (= UIView )

Contrary to other questions about the warning here on stackoverflow I don't set the UITableViewCell 's height directly but want it to be set by its content.与关于 stackoverflow 上警告的其他问题相反,我不直接设置UITableViewCell的高度,而是希望它由其内容设置。 The UIImageView 's image is set at runtime and should keep its shape, so square images should be square and the other rectangular images should be centered horizontally (-> invisible bars left/right, if necessary) but not vertically. UIImageView的图像在运行时设置并应保持其形状,因此方形图像应为方形,其他矩形图像应水平居中(-> 左/右不可见条,如有必要)但不是垂直居中。

Xcode doesn't complain about missing constraints but this layout keeps giving me the infamous "Unable to simultaneously satisfy constraints" warning. 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)>

I already checked with wtfautolayout.com but the only thing wrong that I can see is the height: If the width is 295, then the height should be exactly 50+10+295-40+10=325 (UIStackView.height + constraint in-between + UIView.width - leading/trailing constraints + bottom constraint), not 325.5.我已经检查过wtfautolayout.com但我能看到的唯一错误是高度:如果宽度是 295,那么高度应该正好是 50+10+295-40+10=325(UIStackView.height + 约束在-between + UIView.width - 前导/尾随约束 + 底部约束),而不是 325.5。 Everything else looks fine to me, so I'm not sure why it would calculate that wrong height (and if that's even the problem).其他一切对我来说都很好,所以我不确定为什么它会计算出错误的高度(如果这就是问题的话)。

I already tried a couple of things:我已经尝试了几件事:

  • Added the UIStackView 's fixed height (didn't use it before everything looked fine and it didn't complain about it, still got the warning though)添加了UIStackView的固定高度(在一切看起来都很好之前没有使用它并且它没有抱怨它,但仍然收到警告)
  • UIView.width = UIImageView.height (1:1 aspect ratio, instead of the one above) UIView.width = UIImageView.height (1:1 纵横比,而不是上面的那个)
  • UIImageView.width = UIImageView.height (1:1 aspect ratio, instead of the one above) UIImageView.width = UIImageView.height (1:1 纵横比,而不是上面的那个)
  • Removed the "center x" constraint删除了“中心 x”约束

Nothing worked, I keep getting the constraint warning and if it doesn't show up in portrait mode because of something I changed, then it does as soon as I switch to landscape mode.没有任何效果,我不断收到约束警告,如果由于我更改了某些内容而没有在纵向模式下显示,那么一旦我切换到横向模式,它就会出现。 How do I fix this in IB/without code?如何在 IB/无代码中解决此问题?

尝试将 height=width 约束的优先级更改为High而不是Required

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM