问题
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:
So, I finally found a solution.
Solution is to increase the Hugging Priority of SuperView to High(750 or more) and decrease the Compression Resistance Priority of UIImageView to Low(250 or less). This will let constraints override the image 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.
来源:https://stackoverflow.com/questions/24807474/how-can-i-get-autolayout-to-override-a-uiimageviews-intrinsic-size