NSLayoutAnchor实践

iOS如果项目中不用xib或者storyboard的话,给view做约束一般都是用第三方库Masonry,为什么不用系统提供的AutoLayout呢?因为代码太多不好用

  • 今天给大家介绍一个苹果iOS9后更新的一个布局的好用的类 NSLayoutAnchor

分别用 NSLayoutConstraint NSLayoutAnchorMasonry来进行如下图所示的布局

在进行布局的时候,我会分别讲解下系统 NSLayoutConstraint NSLayoutAnchor两种布局的方法使用和参数说明
至于Masonry的使用 我就不做多介绍了(官方介绍的很清楚,网上资料也不少)

第一种 NSLayoutConstraint

# NSLayoutConstraint 的核心布局方法
[NSLayoutConstraint constraintWithItem:self.redView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];

上面代码的白话文就是
红色view 的 centerX 等于 self.view 的 centerX 1.0倍 加 0
如果还不是很明白的话 看下图就一目了然了

接下来我们来讲一下初始化方法中各个参数的意义:

item:        要布局的view

attribute: 是一个NSLayoutAttribute枚举,可以看到他的枚举值有 left、right、bottom、top 等

relatedBy:  是一个NSLayoutRelation枚举,他的枚举值有 lessThanOrEqual(小于等于)、equal(等于)、greaterThanOrEqual(大于等于), 指定 view1和接下来那个参数 view2两个视图之
间的约束关系的

toItem:     第一个view的参照view 你要参照哪个view 这个就是哪个view(本例中的是self.view控制器view)

attribute:  和第二个参数一样,是来表示第一个视图对第二个视图的
参考位置 ,上下左右 还是 center等

multiplier: 是来计算两个视图之间约束的倍数关系

constant:  是来计算两个视图之间约束的倍数关系的基础上再加一些常量

下面看约束代码

// 布局redView
    NSLayoutConstraint *centerX = [NSLayoutConstraint constraintWithItem:self.redView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
    NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTopMargin multiplier:1.0 constant:10];
    
    NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:self.redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:200];
    NSLayoutConstraint *heith = [NSLayoutConstraint constraintWithItem:self.redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:200];
    
    //可以单个添加约束
    [self.view addConstraint:centerX];
    [self.view addConstraint:top];
    
    //也可以添加约束多个约束
    [self.redView addConstraints:@[width, heith]];
    
    
    
    // 布局blueView
    NSLayoutConstraint *centerX1 = [NSLayoutConstraint constraintWithItem:self.blueView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.redView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
    NSLayoutConstraint *top1 = [NSLayoutConstraint constraintWithItem:self.blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.redView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20];
    
    NSLayoutConstraint *width1 = [NSLayoutConstraint constraintWithItem:self.blueView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.redView attribute:NSLayoutAttributeWidth multiplier:1.5 constant:0];
    NSLayoutConstraint *heith1 = [NSLayoutConstraint constraintWithItem:self.blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.redView attribute:NSLayoutAttributeWidth multiplier:1.5 constant:0];
    
    //iOS8 以后 NSLayoutConstraint 的类方法 也可以把约束添加到视图上,而且省掉了判断添加到那个视图上的问题,避免了上面例子中因为视图添加错误而导致的崩溃
    [NSLayoutConstraint activateConstraints:@[centerX1,top1, width1, heith1]];
NSLayoutConstraint添加约束注意事项

1.如果两个视图(也就是参数 item 和 toItem)是父子关系,设置子控件的约束,约束添加到父控件上
2.如果两个视图(也就是参数 item 和 toItem)是兄弟关系,设置两兄弟的约束,约束会添加到第一个共同的父控件上
3.如果两个视图(也就是参数 item 和 toItem)是同一个视图,约束会添加到自己上(一般给自己添加约束toItem为nil attribute为NSLayoutAttributeNotAnAttribute)
4.要给添加约束的view要设置translatesAutoresizingMaskIntoConstraints为NO 否则约束不生效

    UIView *redView = [[UIView alloc]init];
    redView.backgroundColor = [UIColor redColor];
    // 要禁止 autoresize 意思就是遵循autoLayout抛弃原有设置的高度宽度等
    // 使用autolayout的视图必须要设置该属性
    redView.translatesAutoresizingMaskIntoConstraints = NO;

第二种 NSLayoutAnchor ios9以后

- (NSLayoutConstraint *)constraintEqualToAnchor:(NSLayoutAnchor *)anchor;
- (NSLayoutConstraint *)constraintGreaterThanOrEqualToAnchor:(NSLayoutAnchor *)anchor;
- (NSLayoutConstraint *)constraintLessThanOrEqualToAnchor:(NSLayoutAnchor *)anchor;

/* These methods return an inactive constraint of the form thisAnchor = otherAnchor + constant.
 */
- (NSLayoutConstraint *)constraintEqualToAnchor:(NSLayoutAnchor *)anchor constant:(CGFloat)c;
- (NSLayoutConstraint *)constraintGreaterThanOrEqualToAnchor:(NSLayoutAnchor *)anchor constant:(CGFloat)c;
- (NSLayoutConstraint *)constraintLessThanOrEqualToAnchor:(NSLayoutAnchor *)anchor constant:(CGFloat)c;

接下来我们来讲一下使用方法

比如要给redView设置一个约束,如下

[self.redView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor];

上面代码的意思为 红色的view的centerX和self.view的centerX相等

下面看使用NSLayoutAnchor写的约束代码

    [self.redView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES;
    [self.redView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor constant:20].active = YES;
    [self.redView.widthAnchor constraintEqualToConstant:200].active = YES;
    [self.redView.heightAnchor constraintEqualToConstant:200].active = YES;
    
    [self.blueView.centerXAnchor constraintEqualToAnchor:self.redView.centerXAnchor].active = YES;
    [self.blueView.topAnchor constraintEqualToAnchor:self.redView.bottomAnchor constant:20].active = YES;
    [self.blueView.widthAnchor constraintEqualToAnchor:self.redView.widthAnchor multiplier:1.5].active = YES;
    [self.blueView.heightAnchor constraintEqualToAnchor:self.redView.heightAnchor multiplier:1.5].active = YES;
NSLayoutAnchor添加约束注意事项

1.要给添加约束的view要设置translatesAutoresizingMaskIntoConstraints为NO 否则约束不生效 自定义view的时候也要给子view设置此属性,总之你要给哪个view设置Layout就要给哪个view设置此属性为NO
2.active要设置为YES 这个是控制约束是否真正添加的开关 设置为NO的时候 约束失效

第三种 Masonry

直接上代码了

    [self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_topMargin).with.offset(20);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.height.equalTo(@200);
    }];
    
    [self.blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.redView.mas_bottom).offset(20);
        make.centerX.equalTo(self.redView.mas_centerX);
        make.width.height.equalTo(self.redView).multipliedBy(1.5);
    }];

你可能感兴趣的:(NSLayoutAnchor实践)