有几次,我需要堆叠期望改变高度的 View 。
我编码这个https://github.com/damienromito/UIView-AutoYPositioning
stack dynamic views http://romito.fr/public/images/UIView+AutoYPositioning.jpg
但我认为 NSLayoutConstraint 是最好的解决方案。 有人知道吗?
编辑
使用自动布局以编程方式解决方案:
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/ ) 作为说明
最佳答案
这里是一些关于自动布局的教程。 Raywenderlinch-1 , Raywenderlinch-2 ,
关于ios - 如何使用 Autolayout 确保干净地堆叠多个动态 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22214525/