[英]iOS Image View - Height equal to the size of width
I'm just to create an image that spans the entire screens width. 我只想创建一个跨越整个屏幕宽度的图像。 The image is a square (512x512) and the idea is to scale to the width of the device while maintaining aspect ratio.
图像是正方形(512x512),其目的是在保持纵横比的同时缩放到设备的宽度。 I've tried doing this with contraints, setting the margins to
left: 0, top: 0, right: 0
and keeping the image at aspect-fill
. 我尝试使用约束,将边距设置为
left: 0, top: 0, right: 0
并保持图像处于aspect-fill
。 The issue however, is that the image is becoming larger than the screens width, and thus the image is not only being cut off on the left and right, but is much taller than it should be. 然而,问题是图像变得比屏幕宽度大,因此图像不仅在左侧和右侧被切断,而且比它应该高得多。
How can I properly size an image to have the same width/height has the width of the device? 如何正确调整图像大小以使宽度/高度与设备的宽度相同?
The problem is you didn't set constraints on the image view. 问题是您没有在图像视图上设置约束。 Set the following constraints on image view:
在图像视图上设置以下约束:
1) Set width of image view equal to width of self.view 1)将图像视图的宽度设置为等于self.view的宽度
2) Set aspect ratio 1:1 // This makes sure your image's height is equal to width 2)设置纵横比1:1 //这可确保图像的高度等于宽度
You would need additional constraints to specify the x and y position of the image view. 您需要其他约束来指定图像视图的x和y位置。
Add constraints as shown in screenShot 添加约束,如screenShot中所示
Here for Positioning image view, I have given horizontal and vertical center constraints, that you can change according to your requirements(ie wherever you want to place your imageview.) 这里为定位图像视图,我给出了水平和垂直中心约束,您可以根据您的要求(即您想要放置图像视图的任何位置)进行更改。
Main idea here is to give these two constraints image view - 这里的主要思想是给这两个约束图像视图 -
1.ImageView.width = superview.width
2.ImageView's aspect ratio(width/height) = 1.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.