[英]How to change the height of the view based on width
My views width will change according to the device width but I want to change the height according to that I don't want that to be static. 我的视图宽度将根据设备的宽度而变化,但是我想根据其自身的高度来更改其高度,因为我不希望它是静态的。
I couldn't find any constraint that can take care of this. 我找不到任何可以解决此问题的约束。
I heard aspect ratio can do this. 我听说长宽比可以做到这一点。 But I tried It doesn't seem to work. 但是我尝试了它似乎不起作用。
How it is generally achieved? 一般如何实现?
Use concept of aspect ratio with AutoLayout to update your view with respect to height & width. 在AutoLayout中使用纵横比的概念来更新关于高度和宽度的视图。 Use multiplier to update aspect ratio. 使用乘数更新长宽比。 Here are snap of aspect ration assignment and how to update it's multiplier 这是宽高比分配的快照,以及如何更新它的乘数
Logic of Multiplier : 乘法器逻辑 :
1:1 = Height and width of your image will remain same 1:1 =图片的高度和宽度将保持不变
2:1 = Your width or height is 2x with respect to each other and create a rectangle. 2:1 =彼此的宽度或高度是2x,并创建一个矩形。 Here Height will be 2x or width will be 2x, it depends upon first item and second item in constraint. 在这里,高度将是2x,宽度将是2x,这取决于约束中的第一项和第二项。
Logic of Priority : Priority assigns, priority to specific constraint when it conflicts with another constraint of same view. 优先级逻辑 :当优先级与同一视图的另一个约束冲突时,优先级将优先级分配给特定约束。 eg if you have set a view with 1:1 aspect ratio, with 100 height & 100 width and centred in Main View. 例如,如果您设置的宽高比为1:1的视图,高度为100且宽度为100,并且在主视图中居中。 Now you also assign Left (margin)/Leading constraint as fix value like 200. In that case for different size of screen width, Centred alignment and/or Leading alignment and/or aspect ration will conflict each other. 现在,您还可以将Left(边距)/ Leading约束分配为固定值,例如200。在这种情况下,对于屏幕宽度的不同大小,居中对齐和/或Leading对齐和/或纵横比将相互冲突。 At this point you need to update priority between 0-1000, according to your requirement for adjustment of design. 此时,您需要根据调整设计的要求在0-1000之间更新优先级。
Share me your design, I will give your exact logic, how to use this concept 与我分享您的设计,我将为您提供确切的逻辑,以及如何使用此概念
Try with following easy way.. 尝试以下简单方法。
1) Give Leading
and Trailing
Constraint like below image. 1)给出下图所示的Leading
约束和Trailing
约束 。
2) Now set Top
or Bottom
or Other Constraint as per your design. 2)现在根据您的设计设置“ Top
或“ Bottom
或“其他约束” 。 For me i have settled Vertically Center
. 对我来说,我在Vertically Center
定居了。
3) Now set Height
of UIView
as per your Ratio
which you want in design and after selecting that view
just Drag and Drop
on same view
and select Aspect Ratio
. 3)现在,根据您在设计中想要的Ratio
来设置UIView
Height
,选择该view
后,只需在同一view
上Drag and Drop
然后选择“ Aspect Ratio
。 XCode
will automatically get Ratio Constraint
. XCode
将自动获得Ratio Constraint
。 For ref. 对于参考。 see below image. 见下图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.