iOS技术专题iOS 开发每天分享优质文章ios 知识点

Masonry的使用

2018-06-01  本文已影响5041人  StoneWing
Masonry,是对Auto Layout进行分装的第三方框架,对应的swift版本为Snapkit,Snpkit的简单使用.下面来介绍Masnory的简单使用

Masnory的简单使用:

下面来看一个简单的例子:
- (UILabel *)label {
    if (!_label) {
        _label = [UILabel new];
        [self.view addSubview: _label];
        [_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.mas_equalTo(0); 
            make.centerY.mas_equalTo(self.view);
            make.width.height.mas_greaterThanOrEqualTo(0);
        }];
    }
    return _label;
}

注:

Masonry进阶:(相对某个view进行布局),看下面的例子:

- (UILabel *)leftLb {
    if (!_leftLb) {
        _leftLb = [UILabel new];
        _leftLb.font = [UIFont systemFontOfSize:18];
        _leftLb.backgroundColor = [UIColor redColor];
        [self.view addSubview:_leftLb];
        [_leftLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.height.mas_equalTo(20);
            make.centerY.mas_equalTo(self.view);
        }];
    }
    return _leftLb;
}
- (UILabel *)rightLB {
    if (!_rightLB) {
        _rightLB = [UILabel new];
        _rightLB.font = [UIFont systemFontOfSize:18];
        _rightLB.backgroundColor = [UIColor blueColor];
        [self.view addSubview:_rightLB];
        [_rightLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.leftLb.mas_right);
            make.right.mas_equalTo(0);
            make.centerY.mas_equalTo(self.view);
        }];
    }
    return _rightLB;
}
相对布局
- (UILabel *)leftLb {
    if (!_leftLb) {
        _leftLb = [UILabel new];
        _leftLb.font = [UIFont systemFontOfSize:18];
        _leftLb.backgroundColor = [UIColor redColor];
        [self.view addSubview:_leftLb];
        [_leftLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.height.mas_equalTo(20);
            make.centerY.mas_equalTo(self.view);
        }];
        [_leftLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }
    return _leftLb;
}
左边不拉伸
- (UILabel *)leftLb {
    if (!_leftLb) {
        _leftLb = [UILabel new];
        _leftLb.font = [UIFont systemFontOfSize:18];
        _leftLb.backgroundColor = [UIColor redColor];
        [self.view addSubview:_leftLb];
        [_leftLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.right.mas_lessThanOrEqualTo(self.rightLB.mas_left);
            make.height.mas_equalTo(20);
            make.centerY.mas_equalTo(self.view);
        }];
        [_leftLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }
    return _leftLb;
}
- (UILabel *)rightLB {
    if (!_rightLB) {
        _rightLB = [UILabel new];
        _rightLB.font = [UIFont systemFontOfSize:18];
        _rightLB.backgroundColor = [UIColor blueColor];
        [self.view addSubview:_rightLB];
        [_rightLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_greaterThanOrEqualTo(self.leftLb.mas_right);
            make.right.mas_equalTo(0);
            make.centerY.mas_equalTo(self.view);
        }];
    }
    return _rightLB;
}
左边被压缩
- (UILabel *)leftLb {
    if (!_leftLb) {
        _leftLb = [UILabel new];
        _leftLb.font = [UIFont systemFontOfSize:18];
        _leftLb.backgroundColor = [UIColor redColor];
        [self.view addSubview:_leftLb];
        [_leftLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.right.mas_lessThanOrEqualTo(self.rightLB.mas_left);
            make.height.mas_equalTo(20);
            make.centerY.mas_equalTo(self.view);
        }];
        [_leftLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        [_leftLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }
    return _leftLb;
}
左边不压缩

题外话,Masonry对iPhoneX的处理(不被底部线条遮挡住),比如:

- (UILabel *)bottomLB {
    if (!_bottomLB) {
        _bottomLB = [UILabel new];
        _bottomLB.font = [UIFont systemFontOfSize:18];
        _bottomLB.backgroundColor = [UIColor darkGrayColor];
        _bottomLB.textColor = [UIColor whiteColor];
        _bottomLB.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:_bottomLB];
        [_bottomLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.height.mas_equalTo(50);
        }];
    }
    return _bottomLB;
}
iPhoneX底部
- (UILabel *)bottomLB {
    if (!_bottomLB) {
        _bottomLB = [UILabel new];
        _bottomLB.font = [UIFont systemFontOfSize:18];
        _bottomLB.backgroundColor = [UIColor darkGrayColor];
        _bottomLB.textColor = [UIColor whiteColor];
        _bottomLB.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:_bottomLB];
        [_bottomLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(50);
            make.bottom.mas_equalTo(self.view.mas_bottomMargin);
//            if (@available(iOS 11.0, *)) {
//                make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom);
//            } else {
//                // Fallback on earlier versions
//                make.bottom.mas_equalTo(self.view);
//            }
        }];
    }
    return _bottomLB;
}
iPhoneX底部适配
上一篇下一篇

猜你喜欢

热点阅读