简体   繁体   中英

iOS - AutoLayout 'Unable to simultaneously satisfy constraints'

I am trying to fix these errors I keep getting when running my app.

I have a UITableView that uses prototype cells and I keep getting these errors despite having no errors listed in Xcode Storyboard for constraint problems. Not sure how to read most of these errors so was hoping someone could assist me?

Background UIImageView that should stretch entire cell width/height:

(
    "<NSLayoutConstraint:0x174086c20 UIImageView:0x1741e7100.width == 2.5*UIImageView:0x1741e7100.height>",
    "<NSLayoutConstraint:0x174087530 H:|-(0)-[UIImageView:0x1741e7100]   (Names: '|':UITableViewCellContentView:0x174181e10 )>",
    "<NSLayoutConstraint:0x1740875d0 V:[UIImageView:0x1741e7100]-(0)-|   (Names: '|':UITableViewCellContentView:0x174181e10 )>",
    "<NSLayoutConstraint:0x174087620 H:[UIImageView:0x1741e7100]-(0)-|   (Names: '|':UITableViewCellContentView:0x174181e10 )>",
    "<NSLayoutConstraint:0x174087670 V:|-(0)-[UIImageView:0x1741e7100]   (Names: '|':UITableViewCellContentView:0x174181e10 )>",
    "<NSLayoutConstraint:0x170088890 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x174181e10(375)]>",
    "<NSLayoutConstraint:0x1700888e0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x174181e10(160)]>"
)

UIImageViews (5) that are suppose to be matching width/height, centered on 3rd UIImageView in cell and equal distances apart from each other (when rotated to landscape, these items stay centered and the distance from left/right on first/last cell just increases):

(
    "<NSLayoutConstraint:0x174086e00 Bobblehead_TV.BobbleheadImageView:0x100715ff0.width == 0.5*Bobblehead_TV.BobbleheadImageView:0x100715ff0.height>",
    "<NSLayoutConstraint:0x1740873a0 Bobblehead_TV.BobbleheadImageView:0x100715ff0.width == Bobblehead_TV.BobbleheadImageView:0x1007180e0.width>",
    "<NSLayoutConstraint:0x1740873f0 H:|-(0)-[Bobblehead_TV.BobbleheadImageView:0x100715ff0]   (Names: '|':UITableViewCellContentView:0x174181e10 )>",
    "<NSLayoutConstraint:0x174087490 Bobblehead_TV.BobbleheadImageView:0x100715ff0.top == UIImageView:0x1741e7100.top>",
    "<NSLayoutConstraint:0x1740874e0 Bobblehead_TV.BobbleheadImageView:0x100715ff0.bottom == UIImageView:0x1741e7100.bottom>",
    "<NSLayoutConstraint:0x1740875d0 V:[UIImageView:0x1741e7100]-(0)-|   (Names: '|':UITableViewCellContentView:0x174181e10 )>",
    "<NSLayoutConstraint:0x174087670 V:|-(0)-[UIImageView:0x1741e7100]   (Names: '|':UITableViewCellContentView:0x174181e10 )>",
    "<NSLayoutConstraint:0x174087710 Bobblehead_TV.BobbleheadImageView:0x1007207d0.width == Bobblehead_TV.BobbleheadImageView:0x1007180e0.width>",
    "<NSLayoutConstraint:0x174087800 H:[Bobblehead_TV.BobbleheadImageView:0x100715ff0]-(>=0)-[Bobblehead_TV.BobbleheadImageView:0x1007207d0]>",
    "<NSLayoutConstraint:0x174087850 UITableViewCellContentView:0x174181e10.centerX == Bobblehead_TV.BobbleheadImageView:0x1007180e0.centerX>",
    "<NSLayoutConstraint:0x1740878a0 H:[Bobblehead_TV.BobbleheadImageView:0x1007207d0]-(>=0)-[Bobblehead_TV.BobbleheadImageView:0x1007180e0]>",
    "<NSLayoutConstraint:0x170088890 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x174181e10(375)]>",
    "<NSLayoutConstraint:0x1700888e0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x174181e10(160)]>"
)

Background UIImageView that should stretch entire cell width/height:

You want the image view to stretch the entire cell, but you have a rule UIImageView:0x1741e7100.width == 2.5*UIImageView:0x1741e7100.height that locks the aspect ratio at 1:2.5. You need to remove this rule.

UIImageViews (5) that are suppose to be matching width/height, centered on 3rd UIImageView in cell and equal distances apart from each other (when rotated to landscape, these items stay centered and the distance from left/right on first/last cell just increases):

This one is much more complex, but I will hazard a guess that it's the same aspect ratio problem. This time is Bobblehead_TV.BobbleheadImageView:0x100715ff0.width == 0.5*Bobblehead_TV.BobbleheadImageView:0x100715ff0.height which locks the aspect ratio at 1:0.5.

My Friend,

Reasons could be 1: You have insufficient constraints to satisfy the layout. or 2: You have added more than the required constraints.

As a result of which layout is ambiguous. It is confused and so warning .

My opinion is to recheck all your constraints. And provide minimal constraints but be specific. Thanks

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