如何获得自动布局来覆盖 UIImageView 的固有大小?

How can I get autolayout to override a UIImageView#39;s intrinsic size?(如何获得自动布局来覆盖 UIImageView 的固有大小?)

本文介绍了如何获得自动布局来覆盖 UIImageView 的固有大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UISplitViewController 中的详细视图的自动布局方面遇到了问题.我的视图层次结构包含一个带有装饰元素的 UIImageView,并且图像视图的固有大小似乎搞砸了其余控件的布局.

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.

解决方法是将superview的Hugging Priority提高为.defaultHigh,将UIImageView的Compression Resistance Priority降低为.defaultLow.这将使约束覆盖图像的固有大小.

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 - 此优先级越高,视图越能抵抗增长超过其固有内容大小.

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.

这篇关于如何获得自动布局来覆盖 UIImageView 的固有大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何获得自动布局来覆盖 UIImageView 的固有大小?