@property (nonatomic, strong, readonly) MASConstraint left;
@property (nonatomic, strong, readonly) MASConstraint top;
@property (nonatomic, strong, readonly) MASConstraint right;
@property (nonatomic, strong, readonly) MASConstraint bottom;
@property (nonatomic, strong, readonly) MASConstraint leading;
@property (nonatomic, strong, readonly) MASConstraint trailing;
@property (nonatomic, strong, readonly) MASConstraint width;
@property (nonatomic, strong, readonly) MASConstraint height;
@property (nonatomic, strong, readonly) MASConstraint centerX;
@property (nonatomic, strong, readonly) MASConstraint centerY;
@property (nonatomic, strong, readonly) MASConstraint *baseline;
make.size.mas_equalTo(self.view).offset(-20);
make.centerX.equalTo(self.view.mas_centerX);
make.centerY.equalTo(self.view.mas_centerY);
make.center.mas_equalTo(self.view);
make.size.equalTo(secondview);
两个view就相同大小
等同于
make.width.equalTo(green.mas_width);
make.height.equalTo(green.mas_height);
键盘弹出在消息方法里更新约束:
-(void)keyBoardWillShow:(NSNotification*)noti {
// 获取键盘基本信息(动画时长与键盘高度)
NSDictionary *userInfo = [noti userInfo];
CGRect rect =
[userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGFloat keyboardHeight = CGRectGetHeight(rect);
CGFloat keyboardDuration =
[userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 修改下边距约束
[_textField mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(-keyboardHeight);
}];
// 更新约束
[UIView animateWithDuration:keyboardDuration animations:^{
[self.view layoutIfNeeded];
}];
}
键盘收起时在textField代理方法中再次更新约束
-(void)keyboardWillDisappear:(NSNotification *)noti {
// 获取键盘基本信息(动画时长与键盘高度)
NSDictionary *userInfo = [noti userInfo];
CGRect rect =
[userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
// CGFloat keyboardHeight = CGRectGetHeight(rect);
CGFloat keyboardDuration =[userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[_textField mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(-40);
}];
[UIView animateWithDuration:keyboardDuration animations:^{
[self.view layoutIfNeeded];
}];
}
greenView.backgroundColor = [UIColor greenColor];
greenView.layer.borderWidth = 2;
greenView.layer.borderColor = [[UIColor blackColor]CGColor];
[self.view addSubview:greenView];
UIView *redView = [UIView new];
redView.backgroundColor = [UIColor redColor];
redView.layer.borderWidth =2;
redView.layer.borderColor = [[UIColor blackColor]CGColor];
[self.view addSubview:redView];
UIView *blueView = [UIView new];
blueView.backgroundColor = [UIColor blueColor];
blueView.layer.borderWidth = 2;
blueView.layer.borderColor = [[UIColor blackColor]CGColor];
[self.view addSubview:blueView];
[greenView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(padding);
make.left.mas_equalTo(padding);
make.right.mas_equalTo(redView.mas_left).offset(-padding);
make.bottom.mas_equalTo(blueView.mas_top).offset(-padding);
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.height.mas_equalTo(greenView);
make.bottom.mas_equalTo(-padding);
make.left.mas_equalTo(padding);
make.right.mas_equalTo(-padding);