ios - 如何使用 Autolayout 确保干净地堆叠多个动态 UIView

标签 ios dynamic uiview autolayout nslayoutconstraint

有几次,我需要堆叠期望改变高度的 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/

相关文章:

objective-c - 为什么 Split View Controller 必须始终是您创建的任何界面的根?

javascript - 连续或无限滚动,接近页面底部时加载更多内容

java - 如何动态加载 Java3D 库?

iPhone——UIView的sizeThatFits方法的输入参数

ios - 如何在 iOS 中制作带有底部箭头的提示气球?

ios - UIWebView 很慢

ios - 在 iOS 7 中,如何访问 Interface Builder 中的 topLayoutGuide/bottomLayoutGuide?

c++ - C/C++ 静态库与动态库示例

iphone - 一个 View Controller 可以处理多少个 subview 和 TableView

ios - 未找到 RealmSwift 框架