20
votes

I'm running into a problem with the autolayout for my detail view in a UISplitViewController. My view hierarchy contains a UIImageView with a decorative element, and the image view's intrinsic size seems to be screwing up the layout of the rest of the controls.

These are the constraints I have specified for the image view:

H:[UIImageView:0xc83ba90]-(NSSpace(20))-|
H:|-(NSSpace(20))-[UIImageView:0xc83ba90]
UIImageView:0xc83ba90.bottom == UITextField:0xc83d2d0.bottom
V:[UIImageView:0xc83ba90 (3)]>,
H:[UIImageView:0xc83ba90 (532)] Hug:1 CompressionResistance:750
V:[UIImageView:0xc83ba90 (2)] Hug:250 CompressionResistance:750

The behavior I want is for the image view to be resized to fill its superview, as described by the top two constraints. The actual behavior I'm seeing is that the image view only takes up horizontal space described by its intrinsic size constraint. It also seems to be altering the layout of all of its sibling views, as though the superview were only laying out views in a rectangle as wide as the image view.

I thought that specifying the hugging priority of the image view as low as possible would let the other constraints override it to resize the image view. What am I doing wrong here?

1
UIImageView does not define an intrinsic content size by default. Can you post a picture of IB to let us see the layout? It's hard to get an idea of what you want to do just with text.Guillaume Algis
What part of Interface Builder? There's a lot of controls involved. It looks like the UIImageView is taking its image's size as its intrinsic size.Greg
Having problems with the same issue. Say an image is 300x100. I want it to keep same proportions on an iPad and iPhone. However, its original size override any dynamic constraint. So either looks too small on an iPad or too big on an iPhone.bauerMusic
@Greg, Did you reach to any solution?gunjot singh
@GuillaumeAlgis Your link above refers to UIView, not UIImageView.Robert Atkins

1 Answers

21
votes

So, I finally found a solution.

Solution is to increase the Hugging Priority of the superview to .defaultHigh and decrease the Compression Resistance Priority of the UIImageView to .defaultLow. This will let constraints override the image's Intrinsic Size.

Content Hugging Priority - The higher this priority is, the more a view resists growing larger than its intrinsic content size.

Content Compression Resistance Priority - The higher this priority is, the more a view resists shrinking smaller than its intrinsic content size.