如何确保使用Autolayout清洁堆叠多个动态UIView

时间:2014-03-06 03:53:28

标签: ios dynamic uiview autolayout nslayoutconstraint

有几次,我需要堆叠视图哪个高度有望改变。

我对此https://github.com/damienromito/UIView-AutoYPositioning

进行了编码

stack dynamic views http://romito.fr/public/images/UIView+AutoYPositioning.jpg

但我认为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]];

请参阅本教程(http://commandshift.co.uk/blog/2013/01/31/visual-format-language-for-autolayout/)作为解释

2 个答案:

答案 0 :(得分:1)

以下是一些关于自动布局的教程。 Raywenderlinch-1Raywenderlinch-2

答案 1 :(得分:0)

使用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]];

请参阅本教程(http://commandshift.co.uk/blog/2013/01/31/visual-format-language-for-autolayout/)作为解释

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