自动布局之masonry

网上查找的资料,自己记录一下

masonry 下载地址
添加自动布局  ,里面必须知道控件的宽高,或者相对于其他控件的位置
1、
上左为正 下右为负  是因为坐标而来的 视图坐标左上为原点 X向右为正 Y向下为正

2、方法
mas_makeConstraints 只负责添加约束 AutoLayout不能同时存在两条针对同一对象的约束否则会报错 * mas_updateConstraints 针对上面的情况 会更新在block中出现的约束 不会导致出现两个相同约束的情况 * mas_remakeConstraints 清除之前所有的约束只保留新的约束
* * 三种函数要配合使用 */
(NSArray )mas_makeConstraints:(void(^)(MASConstraintMaker ))block;
(NSArray )mas_updateConstraints:(void(^)(MASConstraintMaker ))block;
(NSArray )mas_remakeConstraints:(void(^)(MASConstraintMaker make))block;

3、Masonry属性

@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;


4、
size 相同
make.size.mas_equalTo(self.view).offset(-20);

5、
居中
 make.centerX.equalTo(self.view.mas_centerX);
 make.centerY.equalTo(self.view.mas_centerY);
等同于:
make.center.mas_equalTo(self.view);

6、
make.size.equalTo(secondview); 两个view就相同大小

等同于

make.width.equalTo(green.mas_width);
make.height.equalTo(green.mas_height);


7、更新约束
初始时约束
[_textField mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(200, 30)); make.bottom.mas_equalTo(-40); make.centerX.equalTo(weakSelf.view.mas_centerX); }];

键盘弹出在消息方法里更新约束:

-(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];
    }];

}


8、例子   上左为正  下右为负 必须在某个约束里面添加  控件的宽高
  UIView *greenView = [UIView new];
    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];
    
    CGFloat padding = 10;
   
    [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);
        //3个控件等高
        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.
height.mas_equalTo(greenView);
        make.
bottom.mas_equalTo(-padding);
        make.
left.mas_equalTo(padding);
        make.
right.mas_equalTo(-padding);
   
    }];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值