简体   繁体   中英

"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:

UIStackView (with 2 UIView s and UILabel s inside) at the top and a UIView (with a UIImageView inside) below it.

The exact constraints are currently:

  • UIStackView : 10pts leading, 10pts trailing, 0pts top, 10pts to the UIView at the bottom, height of 50pts
  • UIView : 20pts leading, 20pts trailing, 10pts bottom, (10pts top from the UIStackView ), height=width
  • UIImageView inside the UIView : 0pts leading/trailing/top/bottom, align center x to 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. 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.

Xcode doesn't complain about missing constraints but this layout keeps giving me the infamous "Unable to simultaneously satisfy constraints" warning.

[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. 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)
  • UIView.width = UIImageView.height (1:1 aspect ratio, instead of the one above)
  • UIImageView.width = UIImageView.height (1:1 aspect ratio, instead of the one above)
  • Removed the "center x" constraint

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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