简体   繁体   English

无论最终图像大小如何,如何为UIImageView设置最大高度

[英]How to set a max height for UIImageView regardless of final Image size

I have an issue where I have a UIImageView with mode as "Aspect Fill". 我有一个问题,我有一个UIImageView模式为“Aspect Fill”。 I have set dimensions for my UIImageView, but I've noticed at runtime, the height of the UIImageView tends to change based on the final image rendered after it's been filled. 我已经为我的UIImageView设置了尺寸,但我注意到在运行时,UIImageView的高度往往会根据填充后渲染的最终图像而改变。

Is there a way to set a max height for the UIImageView regardless of the height of the content that fills it. 有没有办法为UIImageView设置最大高度,无论填充它的内容的高度如何。 Basically what I want is something similar to the kind of functionality we have in CSS whereby I can perform an "overflow: hidden" type of command, so that anything beyond the size of the UIImageView is hidden/ignored. 基本上我想要的是类似于我们在CSS中具有的功能,我可以执行“溢出:隐藏”类型的命令,以便隐藏/忽略超出UIImageView大小的任何内容。

Is there a way to accomplish this in iOS? 有没有办法在iOS中实现这一目标?

Yes, there are two possible causes here: 是的,这里有两个可能的原因:

a) The UIImageView might be growing because of the auto-layout constraints applied to it: If you are using auto-layout just set a constraint to the UIImageView with a maximum (or fixed) height. a)由于应用了自动布局约束,UIImageView可能会增长:如果使用自动布局,只需在UIImageView中设置一个具有最大(或固定)高度的约束。

在此输入图像描述

b) The UIImageView might not be growing at all but you are seeing the "excess" of the image because the view is not clipping it. b)UIImageView可能根本没有增长,但你看到图像的“过剩”,因为视图没有剪切它。 Just set the clipsToBounds to true either on Interface Builder or from your code. 只需在Interface Builder或代码中将clipsToBounds设置为true即可。

Use "Scale to Fill", it will scale the image according to the UIImageView size. 使用“缩放到填充”,它将根据UIImageView大小缩放图像。

"Aspect Fill" will not shrink the UIImageView, but will only print on a part of it. “Aspect Fill”不会缩小UIImageView,但只会在其中的一部分上打印。

I had this issue. 我有这个问题。 i had four views , the top one had an image , the others were three buttons . 我有四个视图顶部有一个图像 ,其他有三个按钮 I put the three buttons in one stack view. 我把三个按钮放在一个堆栈视图中。 and put the image in another stack view . 并将图像放在另一个堆栈视图中 And all i had to do was set the aspect ratio to what i wanted and the magic was done. 而我所要做的就是将纵横比设置为我想要的效果并完成魔术。 and, this was, it was also easy to adapt to small screens or Portrait and Lanscape mode using size-classes or variations . 而且,这也很容易适应小屏幕或纵向和Lanscape模式使用大小类变化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM