iOS自动布局代码实现NSLayout

1.首先需要设置view的属性:translatesAutoresizingMaskIntoConstraints设置为NO。因为会和Autoresizing冲突,不设置会报错。

 2.约束为NSLayoutConstraint类,创建一个约束对象,通过[constraintWithItem:7个参数]这个方法初始化约束对象。

 3.初始化中7个参数说明:

        3-1:constraintWithItem:当前设置约束的对象自己。

        3-2:attribute:当前设置约束的对象自己要约束的属性(上下左右等等)NSLayoutAttributeLeft/Right/Top/Bottom...

        3-3:relatedBy:设置(相等/大于/小于)与相关联的其它view的对应上下左右等属性。

        3-4:toItem:依赖哪个view,就写哪个view。

        3-5:attribute:设置依赖view属性(上下左右等等)。

        3-6:multiplier:设置倍数,是依赖对象的某属性的多少倍。

        3-7:constant:设置偏移量,是依赖对象的某属性的+-多少。

 4.给对象添加约束:

        4-1:如果设置约束的view不依赖于其它view,那么就是自身添加此约束。

        4-2:如果是设置约束的view依赖于其它view:

            4-2-1:如果依赖同级别的view,两者的superview添加该约束。

            4-2-2:如果依赖的是自身的superview,用superview添加该约束。

            4-2-3:如果依赖的是不同系不同superview的view,向上找,用最近的共同的superview添加该约束。

        4-3:这里如果使用了错误的view添加约束,就会报错,一定小心检查确定用哪个view添加约束。

        4-4:添加约束的方法有两种:

            4-4-1:添加单个约束[添加约束的view addConstraint:约束对象];

            4-4-2:添加多个约束[添加约束的view addConstraint:@[约束对象,约束对象,约束对象...]]; 数组类型

5.其它说明:

        5-1:状态栏对象为self.topLayoutGuide

        5-2:如果修改约束或改变约束,想要动画,只需要再动画block内写[xxview layoutIfNeeded];

    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    blueView.backgroundColor = [UIColor blueColor];
    blueView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:blueView];
    
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    redView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:redView];
    
    /*
     代码实现自动布局说明:
     
     1.首先需要设置view的属性:translatesAutoresizingMaskIntoConstraints设置为NO。因为会和Autoresizing冲突,不设置会报错。
     2.约束对象为:NSLayoutConstraint,创建一个约束对象,通过[constraintWithItem:7个参数]这个方法初始化约束对象。
     3.初始化中参数说明:
        3-1:constraintWithItem:当前设置约束的对象自己。
        3-2:attribute:当前设置约束的对象自己要约束的属性(上下左右等等)NSLayoutAttributeLeft/Right/Top/Bottom...
        3-3:relatedBy:设置(相等/大于/小于)与相关联的其它view的对应上下左右等属性。
        3-4:toItem:依赖哪个view,就写哪个view。
        3-5:attribute:设置依赖view属性(上下左右等等)。
        3-6:multiplier:设置倍数,是依赖对象的某属性的多少倍。
        3-7:constant:设置偏移量,是依赖对象的某属性的+-多少。
     4.给对象添加约束:
        4-1:如果设置约束的view不依赖于其它view,那么就是自身添加此约束。
        4-2:如果是设置约束的view依赖于其它view:
            4-2-1:如果依赖同级别的view,两者的superview添加该约束。
            4-2-2:如果依赖的是自身的superview,用superview添加该约束。
            4-2-3:如果依赖的是不同系不同superview的view,向上找,用最近的共同的superview添加该约束。
        4-3:这里如果使用了错误的view添加约束,就会报错,一定小心检查确定用哪个view添加约束。
        4-4:添加约束的方法有两种:
            4-4-1:添加单个约束[添加约束的view addConstraint:约束对象];
            4-4-2:添加多个约束[添加约束的view addConstraint:@[约束对象,约束对象,约束对象...]]; 数组类型
     5.其它说明:
        5-1:状态栏对象为self.topLayoutGuide
        5-2:如果修改约束或改变约束,想要动画,只需要再动画block内写[xxview layoutIfNeeded];
     */


    //blue view布局,左侧20,上侧20,右侧20,高度50;
    NSLayoutConstraint *blueLeft = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:blueView.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
    
    NSLayoutConstraint *blueRight = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView.superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
    
    NSLayoutConstraint *blueTop = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:20];
    
    NSLayoutConstraint *blueHeight = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:80];
    [blueView addConstraint:blueHeight];
    [self.view addConstraints:@[blueLeft,blueRight,blueTop]];

    //redview布局,左侧中心点,上侧20,右侧20,高度同blueview
    
    //左侧leading和父view中线相同---
    NSLayoutConstraint *redLeft = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:redView.superview attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
    //依赖superview,约束加到superview
    [self.view addConstraint:redLeft];
     
    //右侧同blueview相同---依赖blueview
    NSLayoutConstraint *redRight = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
    //依赖blueView,约束加到superview
    [self.view addConstraint:redRight];
    
    //顶部和blueview间隔20---依赖blueview
    NSLayoutConstraint *redTop = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20];
    //依赖blueView,约束加到superview
    [self.view addConstraint:redTop];
    
    //高度同blueview高度---依赖blueview
    NSLayoutConstraint *redHeight = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0.0];
    //依赖blueView,约束加到superview
    [self.view addConstraint:redHeight];

猜你喜欢

转载自blog.csdn.net/JustinZYP/article/details/124477811
今日推荐