第一,基本使用
- (void)viewDidLoad {
[super viewDidLoad];
UIView *greenView = [[UIView alloc] init];
greenView.backgroundColor = [UIColor greenColor];
[self.view addSubview:greenView];
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
CGFloat padding = 10;
[greenView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(padding);
make.top.mas_equalTo(padding);
make.right.mas_equalTo(redView.mas_left).offset(-padding);
make.bottom.mas_equalTo(blueView.mas_top).offset(-padding);
make.height.mas_equalTo(@[redView,blueView]);
make.width.mas_equalTo(redView);
}];
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.height.bottom.mas_equalTo(greenView);
make.right.mas_equalTo(-padding);
make.left.mas_equalTo(greenView.mas_right).offset(padding);
}];
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(-padding);
make.left.mas_equalTo(padding);
make.right.mas_equalTo(-padding);
}];
}
效果图:
第二,简单动画约束
#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()
@property (nonatomic,strong) UIButton *growingButton;
@property (nonatomic,assign) CGFloat scacle;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.growingButton = [UIButton buttonWithType:UIButtonTypeSystem];
[self.growingButton setTitle:@"点击放大" forState:UIControlStateNormal];
self.growingButton.layer.borderColor = [UIColor greenColor].CGColor;
self.growingButton.layer.borderWidth = 3;
[self.view addSubview:self.growingButton];
[self.growingButton addTarget:self action:@selector(onGrowButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
self.scacle = 1.0;
[self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
make.center.mas_equalTo(self.view);
make.width.height.mas_equalTo(100*self.scacle).priorityLow();
make.width.height.lessThanOrEqualTo(self.view);
}];
[self creatUI];
return;
UIView *greenView = [[UIView alloc] init];
greenView.backgroundColor = [UIColor greenColor];
[self.view addSubview:greenView];
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
CGFloat padding = 10;
[greenView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(padding);
make.top.mas_equalTo(padding);
make.right.mas_equalTo(redView.mas_left).offset(-padding);
make.bottom.mas_equalTo(blueView.mas_top).offset(-padding);
make.height.mas_equalTo(@[redView,blueView]);
make.width.mas_equalTo(redView);
}];
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.height.bottom.mas_equalTo(greenView);
make.right.mas_equalTo(-padding);
make.left.mas_equalTo(greenView.mas_right).offset(padding);
}];
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(-padding);
make.left.mas_equalTo(padding);
make.right.mas_equalTo(-padding);
}];
}
-(void)updateViewConstraints
{
[self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
make.center.mas_equalTo(self.view);
make.width.height.mas_equalTo(100*self.scacle).priorityLow();
make.width.height.lessThanOrEqualTo(self.view);
}];
[super updateViewConstraints];
}
-(void)onGrowButtonTaped:(UIButton*)btn
{
self.scacle += 0.2;
[self.view setNeedsUpdateConstraints];
[self.view updateConstraintsIfNeeded];
[UIView animateWithDuration:0.3 animations:^{
[self.view layoutIfNeeded];
}];
}
-(void)creatUI
{
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
[btn1 setTitle:@"按钮1" forState:UIControlStateNormal];
btn1.layer.borderColor = [UIColor greenColor].CGColor;
btn1.layer.borderWidth = 3;
[self.view addSubview:btn1];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
[btn2 setTitle:@"B" forState:UIControlStateNormal];
btn2.layer.borderColor = [UIColor greenColor].CGColor;
btn2.layer.borderWidth = 3;
[self.view addSubview:btn2];
UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeSystem];
[btn3 setTitle:@"C" forState:UIControlStateNormal];
btn3.layer.borderColor = [UIColor greenColor].CGColor;
btn3.layer.borderWidth = 3;
[self.view addSubview:btn3];
[@[btn1,btn2,btn3] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:80 leadSpacing:15 tailSpacing:50];
[@[btn1,btn2,btn3] mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(100);
}];
}