[英]How to resize a fixed width and height UIImageView with auto layout
I have a view named myView
which is always half the screen and sit at the top of screen. 我有一个名为
myView
的视图,该视图始终位于屏幕的一半,并且位于屏幕顶部。
Inside this view I added an UIImageView
with 120x120 size which sits in center of the myView
(horizontally and vertically). 在此视图内,我添加了一个120x120尺寸的
UIImageView
,它位于myView
中心(水平和垂直)。 Inside the IB, to satisfy the constraints(X & Y) I always need to set a fixed width and height for my image, after I set center horizontally and center vertically. 在IB内部,为了满足约束条件(X&Y),在水平居中和垂直居中后,我总是需要为图像设置固定的宽度和高度。
But with a fixed height and width, the image doesn't resize when changing the screen size. 但是使用固定的高度和宽度,更改屏幕尺寸时图像不会调整大小。 I want my image to resize when running on iPhone5 or iPhone 4s, because
myView
will resize. 我希望在iPhone5或iPhone 4s上运行时调整图像大小,因为
myView
会调整大小。 I need something like the image should depend on the myView
size. 我需要类似图像的东西,具体取决于
myView
大小。
How to actually achieve this ? 如何实际实现呢?
set imageView.clipToBounds = yes
, and also set imageView.contentMode = UIViewContentModeScaleAspectFit;
设置
imageView.clipToBounds = yes
,还设置imageView.contentMode = UIViewContentModeScaleAspectFit;
Update You are talking about imageView not the image, in that case it will not resize due to constant width and height, do one thing, create IBoutLet of constraint and change there values when required, or you can also set Aspect ratio with superView 更新您是在谈论imageView而不是图像,在这种情况下,由于恒定的宽度和高度,图像将不会调整大小,做一件事,创建IBoutLet约束并在需要时更改该值,或者您也可以使用superView设置纵横比
If you set the image with fixed width and height it will not change respectively. 如果将图像设置为固定的宽度和高度,则不会分别更改。 You can either set it to be relative to
myView
or you can instead set leading and trailing size from all for edges and delete the center constrains. 您可以将其设置为相对于
myView
,也可以设置所有边缘的前导和尾随尺寸并删除中心约束。
For Swift 4: Updating answer of @Adnan Aftab 对于Swift 4:更新@Adnan Aftab的答案
imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.