简体   繁体   中英

How can I get autolayout to override a UIImageView's intrinsic size?

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?

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.

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