• iOS-屏幕适配-UI布局


    iOS 屏幕适配:autoResizing autoLayout和sizeClass

    一.图片解说

    --------------------------------------------------------------------------------------------------------------------------------------------------------------

    二.AutoLayout

    1.前言  

    •在iOS程序中,大部分视图控制器都包含了大量的代码用于设置UI布局,设置控件的水平或垂直位置,以确保组件在不同版本的iOS中都能得到合理的布局
    •甚至有些程序员希望在不同的设备使用相同的视图控制器,这就给代码添加了更多的复杂性!
    •自动布局AutoLayout的引入很好地解决了这一问题!
     
     

    2.什么是AutoLayout  

    •AutoLayout是一种基于约束的,描述性的布局系统
    –基于约束:和以往定义frame的位置和尺寸不同,AutoLayout的位置确定是以所谓相对位置的约束来定义的,比如x坐标为superView的中心,y坐标为屏幕底部上方10像素等
    –描述性:约束的定义和各个view的关系使用接近自然语言或者可视化语言的方法来进行描述
    –布局系统:用来负责界面的各个元素的位置
    •AutoLayout为开发者提供了一种不同于传统对于UI元素位置指定的布局方法。以前,不论是在IB里拖放,还是在代码中写,每个UIView都会有自己的frame属性,来定义其在当前视图中的位置和尺寸。而使用AutoLayout,就变为了使用约束条件来定义view的位置和尺寸
     

    3.AutoLayout的优势  

    •解决不同分辨率和屏幕尺寸下view的适配问题,同时也简化了旋转时view的位置的定义。原来在底部之上10像素居中的view,不论在旋转屏幕或是更换设备(iPad、iPad mini、iPhone 4或者是iPhone5/iPhone6/iPhone6plus)的时候,始终还在底部之上10像素居中的位置,不会发生变化
    •使用约束条件来描述布局,view的frame会依据这些约束来进行计算
     

    4.AutoLayout和Autoresizing Mask的区别  

    •在iOS6之前,关于屏幕旋转的适配和iPhone,iPad屏幕的自动适配,基本都是由Autoresizing Mask来完成的。但是随着大家对iOS App的要求越来越高,以及今后可能出现的多种屏幕和分辨率的设备,Autoresizing Mask显得有些落伍和迟钝了。AutoLayout可以完成所有原来Autoresizing Mask能完成的工作,同时还能胜任一些原来无法完成的任务,其中包括:
    •AutoLayout可以指定任意两个view的相对位置,而不需要像Autoresizing Mask那样需要两个view在直系的view hierarchy中
    •AutoLayout不必须指定相等关系的约束,它可以指定非相等约束(大于或者小于等);而Autoresizing Mask所能做的布局只能是相等条件的
    •AutoLayout可以指定约束的优先级,计算frame时将优先按照满足优先级高的条件进行计算
     

    5.AutoLayout的基本使用  

    •在创建约束之后,需要将其添加到作用的view上。在添加时要注意目标view需要遵循以下规则
    •1)  对于两个同层级view之间的约束关系,添加到他们的父view上
    •2)   对于两个不同层级view之间的约束关系,添加到他们最近的共同父view上
    •3)  对于有层次关系的两个view之间的约束关系,添加到层次较高的父view上
     

    6.添加和刷新约束(代码)  

    -(void)addConstraint:(NSLayoutConstraint *)constraint

     •刷新约束的改变

    -setNeedsUpdateConstraints

    -layoutIfNeeded


    [button setTranslatesAutoresizingMaskIntoConstraints:NO];

    1
    2
    3
    4
    5
    6
    7
    8
    9
    // 2.1 水平方向的约束
    NSLayoutConstraint *constraintX = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];
     
    [self.view addConstraint:constraintX];
     
    // 2.2 垂直方向的约束
    NSLayoutConstraint *constraintY = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];
     
    [self.view addConstraint:constraintY]; 

    6.使用AutoLayout容易出现的错误 

    •Ambiguous Layout 布局不能确定,即给出的约束条件无法唯一确定一种布局,也就是约束条件不足,无法得到唯一的布局结果。这种情况一般添加一些必要的约束或者调整优先级可以解决
    •Unsatisfiable Constraints 无法满足约束,问题来源是有约束条件互相冲突,因此无法同时满足,需要删掉一些约束
    •现在使用IB可以比较容易地完成复杂约束,在实际开发中很少再会遇到遗漏或者多余约束情况的出现,有问题的约束条件将直接在IB中得到错误或者警告。
     
     三.快速领会VFL语言Demo代码:
    demo代码如下:
    复制代码
    /* Initial views setup */
    
    - (void)setupViews
    {
        self.redView = [UIView new];
        self.redView.translatesAutoresizingMaskIntoConstraints = NO;
        self.redView.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
        
        self.yellowView = [UIView new];
        self.yellowView.translatesAutoresizingMaskIntoConstraints = NO;
        self.yellowView.backgroundColor = [UIColor colorWithRed:1.00 green:0.83 blue:0.58 alpha:1.0];
        
        [self.view addSubview:self.redView];
        [self.view addSubview:self.yellowView];
        
    }
    
    /* 
     Hey Devs... the code in the next functions has to be intended for tutorial purposes only. 
     I have created work-alone examples that contain a lot of code duplication... not a good practice but way easier to explain :P
    */
    
    
    
    
    /* EXAMPLE 1 */
    
    - (void)example_1
    {
        
        // 1. Create a dictionary of views
        NSDictionary *viewsDictionary = @{@"redView":self.redView};
        
        // 2. Define the redView Size
        NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[redView(100)]"
                                                                        options:0
                                                                        metrics:nil
                                                                          views:viewsDictionary];
        
        NSArray *constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[redView(100)]"
                                                                        options:0
                                                                        metrics:nil
                                                                          views:viewsDictionary];
        [self.redView addConstraints:constraint_H];
        [self.redView addConstraints:constraint_V];
        
        // 3. Define the redView Position
        NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[redView]"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:viewsDictionary];
        
        NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[redView]"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:viewsDictionary];
        
        // 3.B ...and try to change the visual format string
        //NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[redView]-30-|" options:0 metrics:nil views:viewsDictionary];
        //NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[redView]" options:0 metrics:nil views:viewsDictionary];
        
        [self.view addConstraints:constraint_POS_H];
        [self.view addConstraints:constraint_POS_V];
    }
    
    
    
    /* EXAMPLE 2 */
    
    - (void)example_2
    {
        
        // 1. Create a dictionary of views
        NSDictionary *viewsDictionary = @{@"redView":self.redView, @"yellowView":self.yellowView};
        
        // 2. Define the views Sizes
        NSArray *red_constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[redView(100)]"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:viewsDictionary];
        NSArray *red_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[redView(100)]"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:viewsDictionary];
        [self.redView addConstraints:red_constraint_H];
        [self.redView addConstraints:red_constraint_V];
        
        NSArray *yellow_constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[yellowView(200)]"
                                                                               options:0
                                                                               metrics:nil
                                                                                 views:viewsDictionary];
        
        NSArray *yellow_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[yellowView(100)]"
                                                                               options:0
                                                                               metrics:nil
                                                                                 views:viewsDictionary];
        [self.yellowView addConstraints:yellow_constraint_H];
        [self.yellowView addConstraints:yellow_constraint_V];
        
        // 3. Define the views Positions
        NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[redView]-40-[yellowView]"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:viewsDictionary];
        
        NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[redView]-10-[yellowView]"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:viewsDictionary];
        
        [self.view addConstraints:constraint_POS_V];
        [self.view addConstraints:constraint_POS_H];
        
    }
    
    
    
    /* EXAMPLE 3 */
    
    - (void)example_3
    {
        
        // 1. Create a dictionary of views
        NSDictionary *viewsDictionary = @{@"redView":self.redView, @"yellowView":self.yellowView};
        
        // 2. Define the views Sizes
        NSArray *red_constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[redView(100)]"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:viewsDictionary];
        
        NSArray *red_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[redView(100)]"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:viewsDictionary];
        [self.redView addConstraints:red_constraint_H];
        [self.redView addConstraints:red_constraint_V];
        
        NSArray *yellow_constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[yellowView(150)]"
                                                                               options:0
                                                                               metrics:nil
                                                                                 views:viewsDictionary];
        
        NSArray *yellow_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[yellowView(100)]"
                                                                               options:0
                                                                               metrics:nil
                                                                                 views:viewsDictionary];
        [self.yellowView addConstraints:yellow_constraint_H];
        [self.yellowView addConstraints:yellow_constraint_V];
        
        // 3. Define the views Positions using options
        NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-120-[redView]"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:viewsDictionary];
        
        NSArray *constraint_POS = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[redView]-10-[yellowView]"
                                                                          options:NSLayoutFormatAlignAllTop
                                                                          metrics:nil views:viewsDictionary];
        
        [self.view addConstraints:constraint_POS_V];
        [self.view addConstraints:constraint_POS];
        
    }
    
    
    
    /* EXAMPLE 4 */
    
    - (void)example_4
    {
        // 1. Create a dictionary of views and metrics
        NSDictionary *viewsDictionary = @{@"redView":self.redView, @"yellowView":self.yellowView};
        NSDictionary *metrics = @{@"redWidth": @100,
                                  @"redHeight": @100,
                                  @"yellowWidth": @100,
                                  @"yellowHeight": @150,
                                  @"topMargin": @120,
                                  @"leftMargin": @20,
                                  @"viewSpacing":@10
                                  };
        
        // 2. Define the views Sizes
        NSArray *red_constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[redView(redHeight)]"
                                                                            options:0
                                                                            metrics:metrics
                                                                              views:viewsDictionary];
        
        NSArray *red_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[redView(redWidth)]"
                                                                            options:0
                                                                            metrics:metrics
                                                                              views:viewsDictionary];
        [self.redView addConstraints:red_constraint_H];
        [self.redView addConstraints:red_constraint_V];
        
        NSArray *yellow_constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[yellowView(yellowHeight)]"
                                                                               options:0
                                                                               metrics:metrics
                                                                                 views:viewsDictionary];
        
        NSArray *yellow_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[yellowView(yellowWidth)]"
                                                                               options:0
                                                                               metrics:metrics
                                                                                 views:viewsDictionary];
        
        [self.yellowView addConstraints:yellow_constraint_H];
        [self.yellowView addConstraints:yellow_constraint_V];
        
        // 3. Define the views Positions
        NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-topMargin-[redView]"
                                                                            options:0
                                                                            metrics:metrics
                                                                              views:viewsDictionary];
        
        NSArray *constraint_POS = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-leftMargin-[redView]-viewSpacing-[yellowView]"
                                                                          options:NSLayoutFormatAlignAllTop
                                                                          metrics:metrics
                                                                            views:viewsDictionary];
        
        [self.view addConstraints:constraint_POS_V];
        [self.view addConstraints:constraint_POS];
    }
    
    
    
    /* EXAMPLE 5 */
    
    - (void)example_5
    {
        // 1. Create a dictionary of views and metrics
        NSDictionary *viewsDictionary = @{@"redView":self.redView};
        NSDictionary *metrics = @{@"vSpacing":@30, @"hSpacing":@10};
        
        // 2. Define the view Position and automatically the Size
        NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-vSpacing-[redView]-vSpacing-|"
                                                                            options:0
                                                                            metrics:metrics
                                                                              views:viewsDictionary];
        
        NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hSpacing-[redView]-hSpacing-|"
                                                                          options:0
                                                                          metrics:metrics
                                                                            views:viewsDictionary];
        
        [self.view addConstraints:constraint_POS_V];
        [self.view addConstraints:constraint_POS_H];
    }
    
    
    
    /* EXAMPLE 6 */
    
    - (void)example_6
    {
        // 1. Create a dictionary of views
        NSDictionary *viewsDictionary = @{@"redView": self.redView, @"yellowView": self.yellowView};
        NSDictionary *metrics = @{@"vSpacing":@30, @"hSpacing":@10};
    
        
        // 2. Define the view Position and automatically the Size (for the redView)
        NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-vSpacing-[redView]-vSpacing-|"
                                                                            options:0
                                                                            metrics:metrics
                                                                              views:viewsDictionary];
        
        NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hSpacing-[redView]-hSpacing-|"
                                                                            options:0
                                                                            metrics:metrics
                                                                              views:viewsDictionary];
        
        [self.view addConstraints:constraint_POS_V];
        [self.view addConstraints:constraint_POS_H];
        
        
        
        // 3. Define sizes thanks to relations with another view (yellowView in relation with redView)
        [self.view addConstraint:[NSLayoutConstraint
                                     constraintWithItem:self.yellowView
                                     attribute:NSLayoutAttributeWidth
                                     relatedBy:NSLayoutRelationEqual
                                     toItem:self.redView
                                     attribute:NSLayoutAttributeWidth
                                     multiplier:0.5
                                     constant:0.0]];
        
        [self.view addConstraint:[NSLayoutConstraint
                                     constraintWithItem:self.yellowView
                                     attribute:NSLayoutAttributeHeight
                                     relatedBy:NSLayoutRelationEqual
                                     toItem:self.redView
                                     attribute:NSLayoutAttributeHeight
                                     multiplier:0.5
                                     constant:0.0]];
        
        // 4. Define position thanks to relations with another view (yellowView in relation with redView)
        [self.view addConstraint:[NSLayoutConstraint
                                        constraintWithItem:self.yellowView
                                        attribute:NSLayoutAttributeCenterX
                                        relatedBy:NSLayoutRelationEqual
                                        toItem:self.redView
                                        attribute:NSLayoutAttributeCenterX
                                        multiplier:1.0
                                        constant:0.0]];
        
        [self.view addConstraint:[NSLayoutConstraint
                                        constraintWithItem:self.yellowView
                                        attribute:NSLayoutAttributeCenterY
                                        relatedBy:NSLayoutRelationEqual
                                        toItem:self.redView
                                        attribute:NSLayoutAttributeCenterY
                                        multiplier:1.0
                                        constant:0.0]];
        
    }
    复制代码
     
    四.用Masonry开源第三方库写VFL语言

    Masonry -- 使用纯代码进行iOS应用的autolayout自适应布局

     

    简介

    简化iOS应用使用纯代码机型自适应布局的工作,使用一种简洁高效的语法替代NSLayoutConstraints.

    • 项目主页: Masonry
    • 最新示例: 点击下载
    • 项目简议: 如果再看到关于纯代码,xib或storyboard,使用哪种方式进行UI布局更合适的讨论,请推荐他们先试用下 Masonry. Masonry,像xib一样快速,同时拥有作为纯代码方式的灵活性 -- github关注度 7800 + 是有原因的!

    快速入门

    安装

    使用 CocoaPods 安装

    pod 'Masonry'

    推荐在你的在 prefix.pch 中引入头文件:

    // 定义这个常量,就可以在使用Masonry不必总带着前缀 `mas_`:
    #define MAS_SHORTHAND
    
    // 定义这个常量,以支持在 Masonry 语法中自动将基本类型转换为 object 类型:
    #define MAS_SHORTHAND_GLOBALS
    
    #import "Masonry.h"

    使用

    初始Masonry

    这是使用MASConstraintMaker创建的约束:

    /* 注意:view1应首先添加为某个视图的子视图,superview是一个局部变量,指view1的父视图. */
    
    UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(superview.mas_top).offset(padding.top);
        make.left.equalTo(superview.mas_left).offset(padding.left);
        make.bottom.equalTo(superview.mas_bottom).offset(-padding.bottom);
        make.right.equalTo(superview.mas_right).offset(-padding.right);
    }];

    甚至可以更短:

    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(superview).insets(padding);
    }];

    不止可以表达相等关系

    .equalTo 等价于 NSLayoutRelationEqual

    .lessThanOrEqualTo 等价于 NSLayoutRelationLessThanOrEqual

    .greaterThanOrEqualTo 等价于 NSLayoutRelationGreaterThanOrEqual

    这三个表达相等关系的语句,可以接受一个参数;此参数可以为以下任意一个:

    1. MASViewAttribute

    make.centerX.lessThanOrEqualTo(view2.mas_left);
    MASViewAttributeNSLayoutAttribute
    view.mas_left NSLayoutAttributeLeft
    view.mas_right NSLayoutAttributeRight
    view.mas_top NSLayoutAttributeTop
    view.mas_bottom NSLayoutAttributeBottom
    view.mas_leading NSLayoutAttributeLeading
    view.mas_trailing NSLayoutAttributeTrailing
    view.mas_width NSLayoutAttributeWidth
    view.mas_height NSLayoutAttributeHeight
    view.mas_centerX NSLayoutAttributeCenterX
    view.mas_centerY NSLayoutAttributeCenterY
    view.mas_baseline NSLayoutAttributeBaseline

    2. UIView/NSView

    如果你需要 view.left 大于或等于label.left:

    // 下面两个约束是完全等效的.
    make.left.greaterThanOrEqualTo(label);
    make.left.greaterThanOrEqualTo(label.mas_left);

    3. NSNumber

    自适应布局允许将宽度或高度设置为固定值.
    如果你想要给视图一个最小或最大值,你可以这样:

    //width >= 200 && width <= 400
    make.width.greaterThanOrEqualTo(@200);
    make.width.lessThanOrEqualTo(@400)

    但是自适应布局不支持将 left,right, centerY等设为固定值.
    如果你给这些属性传递一个常量, Masonry会自动将它们转换为相对于其父视图的相对值:

    //creates view.left = view.superview.left + 10
    make.left.lessThanOrEqualTo(@10)

    除了使用 NSNumber 外,你可以使用基本数据类型或者结构体来创建约束:

    make.top.mas_equalTo(42);
    make.height.mas_equalTo(20);
    make.size.mas_equalTo(CGSizeMake(50, 100));
    make.edges.mas_equalTo(UIEdgeInsetsMake(10, 0, 10, 0));
    make.left.mas_equalTo(view).mas_offset(UIEdgeInsetsMake(10, 0, 10, 0));

    4. NSArray

    一个数组,里面可以混合是前述三种类型的任意几种:

    // 表达三个视图等高的约束.
    make.height.equalTo(@[view1.mas_height, view2.mas_height]);
    make.height.equalTo(@[view1, view2]);
    make.left.equalTo(@[view1, @100, view3.right]);

    约束的优先级

    .priority 允许你指定一个精确的优先级,数值越大优先级越高.最高1000.

    .priorityHigh 等价于 UILayoutPriorityDefaultHigh.优先级值为 750.

    .priorityMedium 介于高优先级和低优先级之间,优先级值在 250~750之间.

    .priorityLow 等价于 UILayoutPriorityDefaultLow, 优先级值为 250.

    优先级可以在约束的尾部添加:

    make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow();
    
    make.top.equalTo(label.mas_top).with.priority(600);

    等比例自适应

    .multipliedBy 允许你指定一个两个视图的某个属性等比例变化

    item1.attribute1 = multiplier × item2.attribute2 + constant,此为约束的计算公式, .multipliedBy本质上是用来限定 multiplier

    注意,因为编程中的坐标系从父视图左上顶点开始,所以指定基于父视图的left或者top的multiplier是没有意义的,因为父视图的left和top总为0.

    如果你需要一个视图随着父视图的宽度和高度,位置自动变化,你应该同时指定 right,bottom,width,height与父视图对应属性的比例(基于某个尺寸下的相对位置计算出的比例),并且constant必须为0.

    // 指定宽度为父视图的 1/4.
    make.width.equalTo(superview).multipliedBy(0.25);

    工具方法

    Masonry提供了一些工具方法来进一步简化约束的创建.

    edges 边界

    //使 top, left, bottom, right等于 view2
    make.edges.equalTo(view2);
    
    //使 top = superview.top + 5, left = superview.left + 10,
    //      bottom = superview.bottom - 15, right = superview.right - 20
    make.edges.equalTo(superview).insets(UIEdgeInsetsMake(5, 10, 15, 20))

    size 尺寸

    // 使宽度和高度大于或等于 titleLabel
    make.size.greaterThanOrEqualTo(titleLabel)
    
    //使 width = superview.width + 100, height = superview.height - 50
    make.size.equalTo(superview).sizeOffset(CGSizeMake(100, -50))

    center 中心

    //使 centerX和 centerY = button1
    make.center.equalTo(button1)
    
    //使 centerX = superview.centerX - 5, centerY = superview.centerY + 10
    make.center.equalTo(superview).centerOffset(CGPointMake(-5, 10))

    你可以使用链式语法来增强代码可读性:

    // 除top外,其他约束都与父视图相等.
    make.left.right.bottom.equalTo(superview);
    make.top.equalTo(otherView);

    更新约束

    有时,你需要修改已经存在的约束来实现动画效果或者移除/替换已有约束.
    在 Masonry 中,有几种不同的更新视图约束的途径:

    1. References 引用

    你可以把 Masonry 语法返回的约束或约束数组,存储到一个局部变量或者类的属性中,以供后续操作某个约束.

    // 声明属性
    @property (nonatomic, strong) MASConstraint *topConstraint;
    
    ...
    
    // when making constraints
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        self.topConstraint = make.top.equalTo(superview.mas_top).with.offset(padding.top);
        make.left.equalTo(superview.mas_left).with.offset(padding.left);
    }];
    
    ...
    // 然后你就可以操作这个属性.
    [self.topConstraint uninstall];

    2. mas_updateConstraints

    如果你只是想添加新的约束,你可以使用便利方法mas_updateConstraints,不需要使用 mas_makeConstraintsmas_updateConstraints,不会移除已经存在的约束(即使新旧约束间相互冲突).

    // 重写视图的updateConstraints方法: 这是Apple推荐的添加/更新约束的位置.
    // 这个方法可以被多次调用以响应setNeedsUpdateConstraints方法.
    // setNeedsUpdateConstraints 可以被UIKit内部调用或者由开发者在自己的代码中调用以更新视图约束. 
    - (void)updateConstraints {
        [self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.width.equalTo(@(self.buttonSize.width)).priorityLow();
            make.height.equalTo(@(self.buttonSize.height)).priorityLow();
            make.width.lessThanOrEqualTo(self);
            make.height.lessThanOrEqualTo(self);
        }];
    
        //根据apple机制,最后应调用父类的updateConstraints方法.
        [super updateConstraints];
    }

    3. mas_remakeConstraints

    mas_remakeConstraintsmas_updateConstraints相似,不同之处在于: mas_remakeConstraints 会先移除视图上已有的约束,再去创建新的约束.

    - (void)changeButtonPosition {
        [self.button mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(self.buttonSize);
    
            if (topLeft) {
            	make.top.and.left.offset(10);
            } else {
            	make.bottom.and.right.offset(-10);
            }
        }];
    }
  • 相关阅读:
    MySQL之数据库优化
    cookie和session
    php自动加载
    php函数之strtr和str_replace的区别
    php函数之substr()
    阶段总结(一)
    json和xml
    sqlserver交换数据行中的指定列
    3 宏、条件编译
    5 常量与变量
  • 原文地址:https://www.cnblogs.com/LifeTechnologySupporter/p/5018185.html
Copyright © 2020-2023  润新知