简体   繁体   English

如何根据宽度更改视图的高度

[英]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后,只需在同一viewDrag 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.

相关问题 如何在iOS中动态更改视图的高度和宽度? - How to dynamically change the height & width of view in iOS? 如何根据设备更改视图高度? (xcode的) - How to change view height based on device? (xcode) 如何更改iOS的相机视图(UIImagePickerViewController)的高度和宽度? - How to change height and width of camera view(UIImagePickerViewController) iOS? 视图高度应根据视图宽度更改:自动版式 - View height should change as per view width: Auto Layout 根据视图更改自定义UITableViewCell高度 - Change custom UITableViewCell height based on view 视图如何在不使用依赖于框架的 intrinsicContentSize 的情况下根据其宽度的 function 确定其高度? - How can a view determine its height based on a function of its width without using an intrinsicContentSize that is dependent on the frame? 如何根据 CGPoint 位置计算高度/宽度 - How to calculate height/width based on CGPoint positions 如何使用特定的宽度和高度视图设置约束 - How to set constraint with a specific width and height view 如何将视图的宽度和高度转换为CGpoint坐标? - How to translate View width and height to CGpoint coordinates? 如何在 SwiftUI 中获取视图或屏幕的高度和宽度 - How to get Height and Width of View or Screen in SwiftUI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM