iOS DeveloperiOS技术专题iOS Developer - AutoLayout

Masonry框架的基本使用

2016-09-10  本文已影响2984人  Bestmer

前言

当我们在storyboard里拖线设置各种约束,本质上每一根线都被编译器转换成了代码。由于使用纯代码设置布局太过麻烦,于是产生了Masonry这个第三方框架,非常好用,功能十分强大。


What is Masonry ?



举个例子


先用苹果官方给出的代码来实现一下

- (void)viewDidLoad {
    [super viewDidLoad];
    //  蓝色View
    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    //  不要将AutoresizingMask转为Autolayout约束
    blueView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:blueView];
    
    //  红色View
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    //  不要将AutoresizingMask转为Autolayout约束
    redView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:redView];
    
    
    //---------------- Blue View ---------------
    //  左边约束
    NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
    [self.view addConstraint:leftConstraint];
    
    //  右边约束
    NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
    [self.view addConstraint:rightConstraint];
    
    //  顶部约束
    NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:20];
    [self.view addConstraint:topConstraint];
    
    //  高度约束
    NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:50];
    [blueView addConstraint:heightConstraint];
    
    //---------------- Red View --------------
    [self.view addConstraints:@[
                                [NSLayoutConstraint constraintWithItem:redView
                                                             attribute:NSLayoutAttributeLeft
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:blueView
                                                             attribute:NSLayoutAttributeCenterX
                                                            multiplier:1.0
                                                              constant:0.0],
                                
                                [NSLayoutConstraint constraintWithItem:redView
                                                             attribute:NSLayoutAttributeRight
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:blueView
                                                             attribute:NSLayoutAttributeRight
                                                            multiplier:1.0 constant:0.0],
                        
                                
                                [NSLayoutConstraint constraintWithItem:redView
                                                             attribute:NSLayoutAttributeTop
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:blueView
                                                             attribute:NSLayoutAttributeBottom
                                                            multiplier:1.0
                                                              constant:20],
                                
                                [NSLayoutConstraint constraintWithItem:redView
                                                             attribute:NSLayoutAttributeHeight
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:blueView
                                                             attribute:NSLayoutAttributeHeight
                                                            multiplier:1.0
                                                              constant:0.0]
   ]];

}


使用Masonry来实现上面的效果


简单讲讲如何手动导入





- (void)viewDidLoad {
    [super viewDidLoad];
    
    //  蓝色View
    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    
    //  红色View
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    
    //  bluView的约束
    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.top.equalTo(self.view).offset(20);
        make.height.equalTo(@50);
    }];
    
    //  redView的约束
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(blueView.mas_centerX);
        make.right.equalTo(blueView.mas_right);
        make.top.equalTo(blueView.mas_bottom).offset(20);
        make.height.equalTo(blueView.mas_height);
    }];
}



三个核心的方法

- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
}
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block {
}
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block {
}




insets表示距离一个视图上左下右的间距

    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(50, 50, 50, 50));
    }];

offset表示距离一个视图有多少的偏移

    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.top.equalTo(self.view).offset(20);
        make.height.equalTo(@50);
    }];

两个很重要的宏

 //define this constant if you want to use Masonry without the 'mas_' prefix
    #define MAS_SHORTHAND

//define this constant if you want to enable auto-boxing for default syntax
    #define MAS_SHORTHAND_GLOBALS

最后

大家如果想深入了解Masonry这个框架,可以查看一下它的源码,一是学习大神的编码风格,比如代码规范的问题;二是学习别人是如何封装的,学习别人的思想。

上一篇下一篇

猜你喜欢

热点阅读