• 纯代码添加约束条件(Auto Layout)


    Auto Layout 是一种基于约束的、描述性的布局系统。也就是使用约束条件来描述布局,View的Frame会根据这些描述来进行计算。

    在iOS6.0以后加入了一个新类: NSLayoutConstraint。 我们以后可以使用可视化格式化语言 Visual Format Language 的方式创建约束:
    #pragma mark - 添加所有控件

    - (void)addAllViews

    {

        // 在延展中先声明leftButton和rightButton两个属性

        // 添加_leftButton

        self.leftButton = [UIButton buttonWithType:UIButtonTypeSystem];

        [_leftButton setTitle:@"Left" forState:UIControlStateNormal];

        _leftButton.layer.borderColor = [UIColor blackColor].CGColor;

        _leftButton.layer.borderWidth = 2.0f;

        [self.view addSubview:_leftButton];

        

        // 添加_rightButton

        self.rightButton = [UIButton buttonWithType:UIButtonTypeSystem];

        [_rightButton setTitle:@"Right" forState:UIControlStateNormal];

        _rightButton.layer.borderColor = [UIColor blackColor].CGColor;

        _rightButton.layer.borderWidth = 2.0f;

        [self.view addSubview:_rightButton];

        

        

        

        // 关闭自动调整

        _leftButton.translatesAutoresizingMaskIntoConstraints = NO;

        _rightButton.translatesAutoresizingMaskIntoConstraints = NO;

        

        // 1. 创建一个存放约束条件的可变数组

        NSMutableArray *tempConstraintsMutableArray = [NSMutableArray array];

        // 2. 在水平方向上:设置_leftButton距离父视图左侧距离为50,_leftButton的宽度为100,_rightButton距离_leftButton的横向距离为50,_rightButton的宽度设置为100

        // 需要理解的参数:@"H:|-50-[_leftButton(==100)]-100-[_rightButton(==100)]"

        [tempConstraintsMutableArray addObjectsFromArray:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:|-50-[_leftButton(==100)]-100-[_rightButton(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_leftButton, _rightButton)]];

        

        // 和上面的设置类似,只是在竖直方向上

        // 在竖直方向上:设置_leftButton距离父视图顶部100,_leftButton高度为100,_rightButton和_leftButton之间的距离为100,_rightButton的高度为100

        // 需要理解的参数:@"V:|-100-[_leftButton(==100)]-50-[_rightButton(==100)]"

    //     [tempConstraintsMutableArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[_leftButton(==100)]-50-[_rightButton(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_leftButton, _rightButton)]];

        

        // 3. 在垂直方向上:给_leftButton设置距离父视图顶部50,_leftButton高度为100

        // 需要理解的参数:@"H:|-100-[_leftButton(==100)]"

        [tempConstraintsMutableArray addObjectsFromArray:[NSLayoutConstraintconstraintsWithVisualFormat:@"V:|-100-[_leftButton(==100)]" options:0metrics:nil views:NSDictionaryOfVariableBindings(_leftButton)]];

        // 在垂直方向上:给_rightButton设置距离父视图顶部50,_rightButton高度为100

        [tempConstraintsMutableArray addObjectsFromArray:[NSLayoutConstraintconstraintsWithVisualFormat:@"V:|-100-[_rightButton(==_leftButton)]"options:0 metrics:nil views:NSDictionaryOfVariableBindings(_rightButton,_leftButton)]];

        

        // 和上面的设置类似,只是在水平方向上的

        // 在水平方向上:设置_leftButton距离父视图左侧为50,_leftButton的宽度为50

    //    [tempConstraintsMutableArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-100-[_leftButton(==50)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_leftButton)]];

        // 在水平方向上:设置_rightButton距离父视图左侧为50,_rightButton的宽度为50

    //    [tempConstraintsMutableArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-100-[_rightButton(==_leftButton)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_rightButton, _leftButton)]];

        

        // 4. 给视图添加约束

        [self.view addConstraints:tempConstraintsMutableArray];

        

         **

         *  解释

         *  H: Horizontal, 水平的,水平方向

         *  V: Vertical, 垂直的,竖直方向

         *

    }

  • 相关阅读:
    swift 获取iphone设备型号
    如何判断静态库是否支持64位
    MAC登录界面多了一个其他账户
    swift4 UIScrollView滑动手势与UIPageViewController冲突解决办法
    swift 保留两位小数以及前面不0
    本地通知
    swift3 UIColor扩展
    swift3 控件创建
    数据库--数据库事务
    数据库---触发器trigger
  • 原文地址:https://www.cnblogs.com/meier1205/p/4591950.html
Copyright © 2020-2023  润新知