#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.创建控件
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
// 2.添加约束
//尺寸:100x100
//位置:父控件右下角,间距20
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.equalTo(@100);
make.right.equalTo(self.view.mas_right).offset(-20);
make.bottom.equalTo(self.view.mas_bottom).offset(-20);
}];
}
/*
//这个方法会将以前的所有约束删掉,添加新的约束
[blueView mas_remakeConstraints:^(MASConstraintMaker *make) {
}];
//这个方法将会覆盖以前的某些约束
[blueView mas_updateConstraints:^(MASConstraintMaker *make) {
}];
*/
@end
代码优化: [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(100);
make.right.bottom.equalTo(self.view).offset(-20);
}];
继续优化: [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(100, 100));
make.right.bottom.mas_equalTo(self.view).offset(-20);
}];