简体   繁体   English

iOS图像视图 - 高度等于宽度的大小

[英]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.

相关问题 为不同的 iOS 设备创建等高和等宽的圆形图像时出错 - Error in Creating a Rounded Image of Equal Height and Width for Different iOS Devices 自动布局:具有相等宽度/高度的正方形图像视图 - Auto Layout: Square Image View with equal width / height 像元高度等于图像宽度 - Cell height equal to image width 如何在不减小ios的高度和宽度的情况下减小图像的大小? - how to reduce the size of image with out reducing height and width in ios? 固定宽度,可变高度的图像视图ios布局 - Fixed width, variable height image view ios layout iOS,scrollView内容大小的高度等于内容视图的高度,但它仍然可以垂直滚动? - iOS, the height of scrollView content size is equal to the content view's height, but it can still scroll vertically? 宽度/高度按钮相等的网格上的iOS约束导致位置和大小变化 - iOS Constraints on Grid of Equal Width/Height Buttons cause positioning and size to vary 如何将视图高度设置为等于其宽度 - How to set a view height to be equal to its width iOS如何根据标签内容大小动态设置堆栈视图宽度 - iOS how to set Stack-view width Dynamically According to Label Content size Height & Width iOS约束。 如何通过约束将图像视高比设置为等于图像比? - iOS Constraints. How to set image view height ration equal to image ratio by constraints?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM