自动布局之masonry

来源:互联网 发布:元数据和数据元区别 编辑:程序博客网 时间:2024/05/21 16:59
网上查找的资料,自己记录一下

masonry 下载地址
 https://github.com/SnapKit/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 = [UIViewnew];
    greenView.
backgroundColor= [UIColorgreenColor];
    greenView.
layer.borderWidth= 2;
    greenView.
layer.borderColor= [[UIColorblackColor]CGColor];
    [
self.viewaddSubview:greenView];
   
   
   
UIView *redView = [UIViewnew];
    redView.
backgroundColor= [UIColorredColor];
    redView.
layer.borderWidth=2;
    redView.
layer.borderColor= [[UIColorblackColor]CGColor];
    [
self.viewaddSubview:redView];
   
   
UIView *blueView = [UIViewnew];
    blueView.
backgroundColor= [UIColorblueColor];
    blueView.
layer.borderWidth= 2;
    blueView.
layer.borderColor= [[UIColorblackColor]CGColor];
    [
self.viewaddSubview: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
原创粉丝点击