-(void)test1CenterView{
UIView *sv = [UIView new];
sv.backgroundColor = [UIColor grayColor];
//在做autoLayout之前 一定要先将view添加到superview上 否则会报错
[self.view addSubview:sv];
[self.view addSubview:self.btn];
[self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));//btn 间距视图sv 各边界距离
// make.center.mas_equalTo(sv);
}];
//mas_makeConstraints就是Masonry的autolayout添加函数 将所需的约束添加到block中行了
[sv mas_makeConstraints:^(MASConstraintMaker *make) {
//将sv居中
make.center.equalTo(self.view);
//将size设置成(300,300)
make.size.mas_equalTo(CGSizeMake(150, 150));
}];
}
-(void)action{
[self.btn mas_updateConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(50, 50));//点击后更改按钮大小,约束会相应更改父视图的大小
}];
}
-(void)test2Offset{
UIView *sv = [UIView new];
sv.backgroundColor = [UIColor grayColor];
//在做autoLayout之前 一定要先将view添加到superview上 否则会报错
[self.view addSubview:sv];
[self.view addSubview:self.btn];
[self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));//btn 间距视图sv 各边界距离
// make.center.mas_equalTo(sv);
}];
//mas_makeConstraints就是Masonry的autolayout添加函数 将所需的约束添加到block中行了
[sv mas_makeConstraints:^(MASConstraintMaker *make) {
//将sv居中
make.center.equalTo(self.view);
make.size.mas_equalTo(CGSizeMake(150, 150));
}];
UIView *sv1 = [UIView new];
sv1.backgroundColor = [UIColor greenColor];
[self.view addSubview:sv1];
[sv1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(sv.mas_bottom).with.offset(10);//底部 10
make.left.equalTo(self.view.mas_left).with.offset(10);//左边 10
make.size.mas_equalTo(CGSizeMake(100, 100));//大小
}];
UIView *sv2 = [UIView new];
sv2.backgroundColor = [UIColor blueColor];
[self.view addSubview:sv2];
[sv2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(sv.mas_top).with.offset(-10);
make.right.equalTo(self.view.mas_right).with.offset(-10);
make.size.mas_equalTo(CGSizeMake(50, 50));
}];
}
-(void)testScrollview{
//自动布局设置contentSize
UIScrollView *scrollView = [UIScrollView new];
scrollView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:scrollView];
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(44,5,5,5));
}];
UIView *container = [UIView new];//中间层 来计算contentSize
[scrollView addSubview:container];
[container mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(scrollView);
make.width.equalTo(scrollView);
}];
int count = 10;
UIView *lastView = nil;
for ( int i = 1 ; i <= count ; ++i )
{
UIView *subv = [UIView new];
[container addSubview:subv];
subv.backgroundColor = [UIColor colorWithHue:( arc4random() % 256 / 256.0 )
saturation:( arc4random() % 128 / 256.0 ) + 0.5
brightness:( arc4random() % 128 / 256.0 ) + 0.5
alpha:1];
[subv mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(container);
make.height.mas_equalTo(@(20*i));
if ( lastView )
{
make.top.mas_equalTo(lastView.mas_bottom);
}
else
{
make.top.mas_equalTo(container.mas_top);
}
}];
lastView = subv;
}
[container mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(lastView.mas_bottom);
}];
}
通过指示器,可以看到contentSize 已经计算好。
参考文章:http://adad184.com/2014/09/28/use-masonry-to-quick-solve-autolayout/