Ios 如何使用Autolayout确保几个动态UIView的干净堆叠

Ios 如何使用Autolayout确保几个动态UIView的干净堆叠,ios,dynamic,uiview,autolayout,nslayoutconstraint,Ios,Dynamic,Uiview,Autolayout,Nslayoutconstraint,有好几次,我需要堆叠期望改变高度的视图 我给这个编码 但我认为NSLayoutConstraint是最好的解决方案。 有人知道吗 编辑 使用autolayout以编程方式解决方案: UIView *one = [[UIView alloc]init]; one.backgroundColor = [UIColor redColor]; one.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:one];

有好几次,我需要堆叠期望改变高度的视图

我给这个编码

但我认为NSLayoutConstraint是最好的解决方案。 有人知道吗

编辑


使用autolayout以编程方式解决方案:

UIView *one = [[UIView alloc]init];
one.backgroundColor = [UIColor redColor];
one.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:one];

UIView *two = [[UIView alloc]init];
two.backgroundColor = [UIColor blueColor];
two.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:two];

UIView *three = [[UIView alloc]init];
three.backgroundColor = [UIColor yellowColor];
three.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:three];

NSDictionary *metrics = @{@"height":@50.0};
NSDictionary *views = NSDictionaryOfVariableBindings(one,two,three);

[self.view addConstraints:[NSLayoutConstraint
                           constraintsWithVisualFormat:@"|-[one]-|"
                           options: 0
                           metrics:metrics
                           views:views]];


[self.view addConstraints:[NSLayoutConstraint
                           constraintsWithVisualFormat:@"V:|-[one]-[two(300)]-[three(100)]-|"
                           options:NSLayoutFormatAlignAllLeft | NSLayoutFormatAlignAllRight
                           metrics:metrics
                           views:views]];
UIView *one = [[UIView alloc]init];
one.backgroundColor = [UIColor redColor];
one.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:one];

UIView *two = [[UIView alloc]init];
two.backgroundColor = [UIColor blueColor];
two.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:two];

UIView *three = [[UIView alloc]init];
three.backgroundColor = [UIColor yellowColor];
three.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:three];

NSDictionary *metrics = @{@"height":@50.0};
NSDictionary *views = NSDictionaryOfVariableBindings(one,two,three);

[self.view addConstraints:[NSLayoutConstraint
                           constraintsWithVisualFormat:@"|-[one]-|"
                           options: 0
                           metrics:metrics
                           views:views]];


[self.view addConstraints:[NSLayoutConstraint
                           constraintsWithVisualFormat:@"V:|-[one]-[two(300)]-[three(100)]-|"
                           options:NSLayoutFormatAlignAllLeft | NSLayoutFormatAlignAllRight
                           metrics:metrics
                           views:views]];

请参考本教程()作为说明

以下是一些有关自动布局的教程。

使用autolayout以编程方式解决方案:

UIView *one = [[UIView alloc]init];
one.backgroundColor = [UIColor redColor];
one.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:one];

UIView *two = [[UIView alloc]init];
two.backgroundColor = [UIColor blueColor];
two.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:two];

UIView *three = [[UIView alloc]init];
three.backgroundColor = [UIColor yellowColor];
three.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:three];

NSDictionary *metrics = @{@"height":@50.0};
NSDictionary *views = NSDictionaryOfVariableBindings(one,two,three);

[self.view addConstraints:[NSLayoutConstraint
                           constraintsWithVisualFormat:@"|-[one]-|"
                           options: 0
                           metrics:metrics
                           views:views]];


[self.view addConstraints:[NSLayoutConstraint
                           constraintsWithVisualFormat:@"V:|-[one]-[two(300)]-[three(100)]-|"
                           options:NSLayoutFormatAlignAllLeft | NSLayoutFormatAlignAllRight
                           metrics:metrics
                           views:views]];
UIView *one = [[UIView alloc]init];
one.backgroundColor = [UIColor redColor];
one.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:one];

UIView *two = [[UIView alloc]init];
two.backgroundColor = [UIColor blueColor];
two.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:two];

UIView *three = [[UIView alloc]init];
three.backgroundColor = [UIColor yellowColor];
three.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:three];

NSDictionary *metrics = @{@"height":@50.0};
NSDictionary *views = NSDictionaryOfVariableBindings(one,two,three);

[self.view addConstraints:[NSLayoutConstraint
                           constraintsWithVisualFormat:@"|-[one]-|"
                           options: 0
                           metrics:metrics
                           views:views]];


[self.view addConstraints:[NSLayoutConstraint
                           constraintsWithVisualFormat:@"V:|-[one]-[two(300)]-[three(100)]-|"
                           options:NSLayoutFormatAlignAllLeft | NSLayoutFormatAlignAllRight
                           metrics:metrics
                           views:views]];
请参考本教程()作为说明


为了帮助我,我为UIView开发了一个类别:(能够隐藏/显示自动布局视图)

您可以使用自动布局解决这个问题,这要求您停止考虑矩形,开始考虑关系。在您的示例中,您需要告诉autolayout每个视图相对于其他视图的位置,并根据内容的大小设置大小约束。是的,我刚刚通过本精彩教程了解了所有这些。要知道的主要事情是,在自动布局世界中,“initWithFrame”:不再存在!非常感谢。