IOS_编程思想_Masonry的简单分析

#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
//约束
//Block作为参数 ---- 函数式编程思想
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
//make ---- 链式编程
        make.left.top.equalTo(@10);
        make.right.bottom.equalTo(@-10);
    }];
}
@end

mas_makeConstraints执行的流程:

- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
     self.translatesAutoresizingMaskIntoConstraints = NO;
     MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
     block(constraintMaker);
     return [constraintMaker install];
     }
self.translatesAutoresizingMaskIntoConstraints = NO;
***关掉原来旧版本的自动布局(Autoresizing)
MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
1.创建约束制造者MASConstraintMaker,并且绑定控件
block(constraintMaker);
2.执行mas_makeConstraints传入的Block
[constraintMaker install];
3.让约束制造者安装约束!
- (NSArray *)install {
     if (self.removeExisting) {
     NSArray *installedConstraints = [MASViewConstraint installedConstraintsForView:self.view];
     for (MASConstraint *constraint in installedConstraints) {
     [constraint uninstall];
     }
     }
     NSArray *constraints = self.constraints.copy;
     for (MASConstraint *constraint in constraints) {
     constraint.updateExisting = self.updateExisting;
     [constraint install];
     }
     [self.constraints removeAllObjects];
     return constraints;
     }
[constraint uninstall];
          *1.清空之前所有的约束
[constraint install];
          *2.遍历约束数组,安装约束
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容