20

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?

Greg
  • 10,360
  • 6
  • 44
  • 67
  • 2
    [`UIImageView` does not define an intrinsic content size by default](https://stackoverflow.com/questions/14528172/uiview-and-intrinsiccontentsize). 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 Jul 17 '14 at 20:30
  • 1
    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 Aug 01 '14 at 22:46
  • 2
    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 Oct 04 '14 at 04:51
  • @Greg, Did you reach to any solution? – gunjot singh Aug 09 '16 at 10:19
  • 1
    @GuillaumeAlgis Your link above refers to `UIView`, not `UIImageView`. – Robert Atkins Feb 16 '21 at 10:12

1 Answers1

27

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.

shim
  • 9,289
  • 12
  • 69
  • 108
gunjot singh
  • 2,578
  • 20
  • 28