1.边距
[bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(0) ;
}] ;
在和父视图比较时,可以省略掉父视图,如下:
[bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(10) ;
}] ;
需要注意的是必须在和父视图同样方向比较时才可以省略,比如子视图的左边和父视图的左边做约束时才可以省略,子视图的左边和父视图的右边做约束时则不可以比较。
2.内边距
[bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0)) ;
}] ;
上面的约束等价于
[bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.bottom.right.equalTo(self.view).offset(0) ;
}] ;
3.约束动画
约束做动画相比之下比frame做动画要复杂一点。
@interface ViewController ()
{
CGFloat _scale ;
UIButton* _button ;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor] ;
_scale=1.0 ;
_button=[UIButton buttonWithType:UIButtonTypeCustom] ;
_button.backgroundColor=[UIColor blueColor] ;
[_button addTarget:self action:@selector(clickBlue) forControlEvents:UIControlEventTouchUpInside] ;
[self.view addSubview:_button] ;
[_button mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.width.offset(100*_scale).priorityLow() ;
make.center.equalTo(self.view) ;
make.width.height.lessThanOrEqualTo(self.view) ;
}] ;
}
-(void)clickBlue
{
_scale+=0.2 ;
//告诉系统需要更新约束
[self.view setNeedsUpdateConstraints] ;
// 调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用
[self.view updateConstraintsIfNeeded] ;
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded] ;
}] ;
}
// 你所需要做的动画都在下面这个方法中写
-(void)updateViewConstraints
{
[super updateViewConstraints] ;
[_button mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.width.offset(100*_scale).priorityLow() ;
make.center.equalTo(self.view) ;
make.height.width.lessThanOrEqualTo(self.view) ;
}] ;
}
@end