如何在 iOS 的自动布局中设置视图的动态宽度和高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30777528/
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 06:21:42  来源:igfitidea点击:

How to set dynamic width & height of a view in autolayout in iOS?

iosobjective-cxcodemacosautolayout

提问by TechChain

I have a UIView in my super view.I have set it some constraints.Now I want the size of view should be different on different devices.If I set the fix width & height then I get the wrong result.I have tried to user aspect ratio but that make a view too big or too small on multiple devices.

我的超级视图中有一个 UIView。我已经给它设置了一些约束。现在我希望视图的大小在不同的设备上应该不同。如果我设置了固定宽度和高度,那么我得到了错误的结果。我尝试过用户纵横比,但这会使多个设备上的视图过大或过小。

I want the height & width should increase in equal proportion that it must look same all devices.I want to have flexible height and width of the view.Please tell how to do this?

我希望高度和宽度应该以相等的比例增加,它必须在所有设备上看起来都一样。我想要灵活的高度和宽度的视图。请告诉如何做到这一点?

Here is the image enter image description here

这是图像 在此处输入图片说明

Thanks in Advance

提前致谢

回答by Andrew Luca

Use Size Classes. It helps to have different settings for different devices. http://www.raywenderlich.com/83276/beginning-adaptive-layout-tutorialhttps://youtu.be/IwSTXY0awng?t=2m12s

使用尺寸类。它有助于为不同的设备进行不同的设置。 http://www.raywenderlich.com/83276/beginning-adaptive-layout-tutorial https://youtu.be/IwSTXY0awng?t=2m12s

回答by Mukesh

enter image description hereThe most simple way is create a Outlet of your widthand heightconstraint.

在此处输入图片说明最简单的方法是创建一个你widthheight约束的出口。

Then dynamically change it in viewDidLoadaccording to phone.

然后viewDidLoad根据手机动态更改。

To check which phone it is u can check the device height like 480for iPhone 4S ,568for iPhone 5,5S and etc

要检查它是哪部手机,您可以检查设备高度,例如480iPhone 4S、568iPhone 5,5S 等

@interface ScoreListViewController (){
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *widthConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;

@end



- (void)viewDidLoad {
[super viewDidLoad];
// Set the value according to your requirement
if(4S){
self.heightConstraint.constant=100;
}

 }

回答by Nischal Hada

Try to follow the screen shots as below:

尝试按照以下屏幕截图进行操作:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

You will get the result as below

你会得到如下结果

enter image description here

在此处输入图片说明

回答by deltaaruna

I have written a blog articleabout autolayouting for dynamically changing layouts.
For each screen size you can programmatically add the constraints.
Algorithm

我写了一篇关于动态更改布局的自动布局的博客文章
对于每个屏幕尺寸,您可以以编程方式添加约束。
算法

  1. Get the current device size
  2. Get the required constraints according to the device size
  3. Then programmatically add the constraints
  1. 获取当前设备大小
  2. 根据设备尺寸获取所需约束
  3. 然后以编程方式添加约束


Changing according to orientation


根据方向变化

  • Listen to the UIDeviceOrientationDidChangeNotification notifiation
  • Add new constraints according to the notification
  • Then call setNeedsUpdateConstraints
    For more info and a example code, look at the blog article and thisgithub repo
  • 收听 UIDeviceOrientationDidChangeNotification 通知
  • 根据通知添加新的约束
  • 然后调用 setNeedsUpdateConstraints
    有关更多信息和示例代码,请查看博客文章和这个github repo