简体   繁体   English

UIImageView / UIView的Autolayout纵横比

[英]Autolayout aspect ratio for UIImageView / UIView

How to set autolayout for UIImageView /UIView so that the image view width and height increases a bit for iphone 6 and 6 plus. 如何为UIImageView / UIView设置autolayout,以便iphone 6和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. 无论如何设置约束高度和图像视图的宽度可以手动设置为iPhone 6和6加。 Take a look at the screenshot the image view in iphone 6 and 6 plus should be little smaller than it is showing now. 看一下屏幕截图,iphone 6和6 plus中的图像视图应该比它现在显示的要小一些。 在此输入图像描述

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. 关系将与superview关联,然后您可以通过添加自己的乘数来更改关系。

What you need to do exactly: 你需要做什么:

1) Select your image view. 1)选择图像视图。

2) Hold down the control key. 2)按住控制键。 While you are holding it, drag your mouse from the image view to the outer view. 在按住它时,将鼠标从图像视图拖动到外部视图。

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)使用乘数进行播放,直到您对预览屏幕上的结果感到满意为止。

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. 在这种情况下,使用具有公式I的相等宽度和相等高度约束将通过示例解释如下。

Say i have a UIView taking whole of the width of iPhone 5 ie 320 points. 假设我有一个UIView占据iPhone 5的整个宽度,即320点。 (remember 1 point = 2 pixel for retina display) and a height of 46 points. (记住1点=视网膜显示的2像素)和46点的高度。 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. 我希望在这个UIView中有一个按钮(宽度= 30点,高度= 60点),它会根据具有相同宽高比的不同手机调整大小。 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. 我将控制从按钮拖动到UIView并首先选择相等的宽度然后将使用Equal高度再次执行相同的操作。 Following this I will select the constraint and will make sure that the first item is the button and second item is the UIView. 接下来,我将选择约束,并确保第一项是按钮,第二项是UIView。 I will use this formula to calculate the multiplier then: 我将使用此公式来计算乘数:

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屏幕按钮的大小。

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

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