ios UIImageView / UIView 的自动布局纵横比

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27742897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:13:01  来源:igfitidea点击:

Autolayout aspect ratio for UIImageView / UIView

iosuiviewuiimageviewautolayoutxib

提问by abdul sathar

How to set autolayout for UIImageView /UIView so that the image view width and height increases a bit for iphone 6 and 6 plus. I have tried adding the constraint Aspect ratio but the increased height and width are very big. Is there anyway to set the constraint height and width of image view can be set manually for iphone 6 and 6 plus. Take a look at the screenshot the image view in iphone 6 and 6 plus should be little smaller than it is showing now. enter image description here

如何为 UIImageView /UIView 设置自动布局,以便 iphone 6 和 6 plus 的图像视图宽度和高度增加一点。我曾尝试添加约束纵横比,但增加的高度和宽度非常大。无论如何,可以为iphone 6和6 plus手动设置图像视图的约束高度和宽度。看看屏幕截图,iphone 6 和 6 plus 中的图像视图应该比现在显示的要小一点。 在此处输入图片说明

回答by Tzegenos

As I understood your problem is that you want to control how much the image will be increased in each different screen size. You can control everything. What you need to do is to assign an equals width constraint on your image. Preferable on image's width. The relation will be with the superview and then you can change the relation by adding your own Multiplier.

据我了解,您的问题是您想控制在每个不同的屏幕尺寸中图像的增加量。你可以控制一切。您需要做的是在您的图像上分配一个等宽约束。最好是图像的宽度。关系将与超级视图相关,然后您可以通过添加自己的乘数来更改关系。

What you need to do exactly:

你需要做什么:

1) Select your image view.

1) 选择您的图像视图。

2) Hold down the control key. While you are holding it, drag your mouse from the image view to the outer view.

2) 按住控制键。按住它时,将鼠标从图像视图拖到外部视图。

3) Select 'Equals width'.

3) 选择“等于宽度”。

4) Double click on your constraint in the 'Constraints' tab.

4) 在“约束”选项卡中双击您的约束。

5) Ensure that the image view is presented as the first item, if not click on the first item and select 'Reverse first and second item'.

5) 确保图像视图显示为第一项,如果不是,请单击第一项并选择“反转第一项和第二项”。

6) Play with the multiplier until you are satisfied with the result on the preview screen.

6) 使用乘数进行游戏,直到您对预览屏幕上的结果感到满意为止。

回答by Ankit Khanna

Sometimes playing with aspect ratio constraint doesn't help. In such a condition use equal width and equal height constraint with a formula I will explain as below with an example.

有时玩纵横比约束无济于事。在这种情况下,请使用等宽和等高约束以及公式,我将在下面举例说明。

Say i have a UIView taking whole of the width of iPhone 5 i.e. 320 points. (remember 1 point = 2 pixel for retina display) and a height of 46 points. I want a button (Width = 30 point, height = 60 points) inside this UIView which would resize as per different phones with the same aspect ratio. I will do Control drag from the button to the UIView and select equal width first time and then will do the same thing again with Equal heights. Following this I will select the constraint and will make sure that the first item is the button and second item is the UIView. I will use this formula to calculate the multiplier then:

假设我有一个 UIView 占据 iPhone 5 的整个宽度,即 320 点。(记住 1 点 = 2 像素用于视网膜显示)和 46 点的高度。我想要一个按钮(宽度 = 30 点,高度 = 60 点)在这个 UIView 中,它会根据具有相同纵横比的不同手机调整大小。我将控制从按钮拖动到 UIView 并第一次选择等宽,然后再次使用等高做同样的事情。在此之后,我将选择约束并确保第一项是按钮,第二项是 UIView。我将使用这个公式来计算乘数,然后:

To calculate width multiplier: 60 (width of the button) / 320 (width of the UIView) * 100 = 18.75%

计算宽度乘数:60(按钮的宽度)/ 320(UIView 的宽度)* 100 = 18.75%

To calculate height multiplier: 30 (height of the button) / 46 (height of the UIView) * 100 = 65.217%

计算高度乘数:30(按钮的高度)/ 46(UIView 的高度)* 100 = 65.217%

Now enter this multiplier (remember to use %age sign as well just like above) and this should resize the button for different sizes of iPhone screen.

现在输入这个乘数(记住像上面一样使用 %age 符号),这应该为不同尺寸的 iPhone 屏幕调整按钮的大小。