测试测试过 | ✗ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
发布最后发布 | 2015年3月 |
由Damien Romito维护。查看详情.
创建和更新自动布局约束的简便方法(主要用于更新UIView的宽度和高度)
1 - 导入类别
#import "UIView+UpdateAutoLayoutConstraints.h"
2 - 创建你的UIView
UIView *myView1 = [[UIView alloc]init];
one.backgroundColor = [UIColor redColor];
one.translatesAutoresizingMaskIntoConstraints = NO; //<<-- Don't forget this line to enable AutoLayout
[self.view addSubview:one];
UIView *myView2 = [[UIView alloc]init];
two.backgroundColor = [UIColor blueColor];
two.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:two];
3 - 创建初始约束
NSDictionary *metrics = @{@"height":@50.0};
NSDictionary *views = NSDictionaryOfVariableBindings(myView1,myView2);
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"|-[one]-|"
options: 0
metrics:metrics
views:views]];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|-[myView1(50)][myView2]]"
options:NSLayoutFormatAlignAllLeft | NSLayoutFormatAlignAllRight
metrics:metrics
views:views]];
4 - 当你需要时,更新这个约束
//Hide View
[myView1 setConstraintConstant:0 forAttribute:NSLayoutAttributeHeight];
//if constraint doesn't exist, it will be created
[myView1 setConstraintConstant:20 forAttribute:NSLayoutAttributeWidth];
//you can use tools to hide/show a uiview
[myView1 hideByHeight:YES];
//then
[myView1 hideByHeight:NO];