Tometoyou Tometoyou - 18 days ago 6
Swift Question

Which constraints are conflicting?

I have a UICollectionViewCell that keeps giving me an error for conflicting autolayout constraints. The following is the constraints it prints out in the debugger that it claims are conflicting:

(
"<NSAutoresizingMaskLayoutConstraint:0x10e47df70 h=--& v=--& UIView:0x1094594c0.width == 414 (active)>",
"<NSAutoresizingMaskLayoutConstraint:0x10e47e110 h=--& v=--& UIView:0x1094594c0.height == 531 (active)>",
"<NSLayoutConstraint:0x10947e7e0 UIButton:0x10947e410.height == 44 (active)>",
"<NSLayoutConstraint:0x10e457fd0 UIImageView:0x10e44e960.height == 1.2*UIImageView:0x10e44e960.width (active)>",
"<NSLayoutConstraint:0x109488520 V:|-(0)-[UIImageView:0x10e44e960] (active, names: '|':UIView:0x10947bfd0 )>",
"<NSLayoutConstraint:0x109488610 H:[UIImageView:0x10e44e960]-(0)-| (active, names: '|':UIView:0x10947bfd0 )>",
"<NSLayoutConstraint:0x109488700 H:|-(0)-[UIImageView:0x10e44e960] (active, names: '|':UIView:0x10947bfd0 )>",
"<NSLayoutConstraint:0x1094890c0 V:[UIImageView:0x10e44e960]-(8)-[UITextView:0x102940a00'Hey'] (active)>",
"<NSLayoutConstraint:0x10948a520 V:[UIButton:0x10947e410]-(0)-| (active, names: '|':UIView:0x10947bfd0 )>",
"<NSLayoutConstraint:0x10948a820 V:[UITextView:0x102940a00'Hey']-(8)-[UIButton:0x10947e410] (active)>",
"<NSLayoutConstraint:0x10948ae00 V:|-(5)-[UIView:0x10947bfd0] (active, names: '|':UIView:0x109452f60 )>",
"<NSLayoutConstraint:0x10948af40 H:|-(5)-[UIView:0x10947bfd0] (active, names: '|':UIView:0x109452f60 )>",
"<NSLayoutConstraint:0x10948b030 V:[UIView:0x10947bfd0]-(5)-| (active, names: '|':UIView:0x109452f60 )>",
"<NSLayoutConstraint:0x10948b120 H:[UIView:0x10947bfd0]-(5)-| (active, names: '|':UIView:0x109452f60 )>",
"<NSLayoutConstraint:0x10948b500 H:[UIView:0x109452f60]-(0)-| (active, names: '|':UIView:0x1094594c0 )>",
"<NSLayoutConstraint:0x10948b640 H:|-(0)-[UIView:0x109452f60] (active, names: '|':UIView:0x1094594c0 )>",
"<NSLayoutConstraint:0x10948b730 V:|-(0)-[UIView:0x109452f60] (active, names: '|':UIView:0x1094594c0 )>",
"<NSLayoutConstraint:0x10948b820 V:[UIView:0x109452f60]-(0)-| (active, names: '|':UIView:0x1094594c0 )>"
)


It then tries to solve the issue by breaking the following constraint:

<NSLayoutConstraint:0x10e457fd0 UIImageView:0x10e44e960.height == 1.2*UIImageView:0x10e44e960.width (active)>


I'm having real trouble trying to identify what exactly is conflicting as my XIB doesn't show any errors.

Answer

You have a constraint that sets the aspect ratio of the image to 1.2, so the height of the image must be 1.2 times the size of the width.

But you also constrain the image to be the same size as its parent view minus some fixed space for some controls. This view in turn is set to be the same size as its parent view minus a 5 unit border and this third view is the same size as the outermost view which has a fixed size of 414 x 531.

I expect, once all that is worked out, the image cannot be made 1.2 times taller than it is wide.

I would try deleting the horizontal constraints for the image to its parent view edges and adding a new one to centre it horizontally and see how that goes.