zoukankan      html  css  js  c++  java
  • IOS手动添加的View 在代码中使用(自动布局)autoLayout

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        UIButton *btnTest = [UIButton buttonWithType:UIButtonTypeCustom];//不需要去刻意指定x,y的坐标,可以用CGRectZero
        btnTest.backgroundColor = [UIColor redColor];
        btnTest.layer.borderColor = [UIColor yellowColor].CGColor;
        btnTest.layer.borderWidth = 2.0;
        [self.view addSubview:btnTest];
        
        
        [btnTest setTranslatesAutoresizingMaskIntoConstraints:NO];//将使用AutoLayout的方式布局
        //btnTest顶部相对于self.view的顶部距离为100
        NSLayoutConstraint *constraintTop = [NSLayoutConstraint constraintWithItem:btnTest attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:100];
        //btnTest左侧相对于self.view的左侧距离为100
        NSLayoutConstraint *constraintLeft = [NSLayoutConstraint constraintWithItem:btnTest  attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:100];
        //btnTest右侧相对于self.view的右侧距离为100
        NSLayoutConstraint *constraintRight = [NSLayoutConstraint constraintWithItem:self.view  attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:btnTest attribute:NSLayoutAttributeRight multiplier:1.0 constant:100];
        //btnTest底部相对于self.view的底部距离为100
        NSLayoutConstraint *constraintBottom = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:btnTest attribute:NSLayoutAttributeBottom multiplier:1.0 constant:100];
        //水平居中
        NSLayoutConstraint *constraintXCenter = [NSLayoutConstraint constraintWithItem:btnTest attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:00.0f];
        //垂直居中
         NSLayoutConstraint *constraintYCenter = [NSLayoutConstraint constraintWithItem:btnTest attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:00.0f];
        
        //将约束添加到父视图中
        [self.view addConstraint:constraintTop];
        [self.view addConstraint:constraintLeft];
        [self.view addConstraint:constraintRight];
        [self.view addConstraint:constraintBottom];
        [self.view addConstraint:constraintXCenter];
        [self.view addConstraint:constraintYCenter];
        
    }
    

      

  • 相关阅读:
    [LeetCode] 139. Word Break 单词拆分
    [LeetCode] 140. Word Break II 单词拆分II
    [LeetCode] 297. Serialize and Deserialize Binary Tree 二叉树的序列化和反序列化
    [LeetCode] 206. Reverse Linked List 反向链表
    [LeetCode] 92. Reverse Linked List II 反向链表II
    [LeetCode] 258. Add Digits 加数字
    [LeetCode] 66. Plus One 加一
    [LeetCode] 21. Merge Two Sorted Lists 合并有序链表
    [LeetCode] 88. Merge Sorted Array 合并有序数组
    [LeetCode] 10. Regular Expression Matching 正则表达式匹配
  • 原文地址:https://www.cnblogs.com/mgbert/p/4067178.html
Copyright © 2011-2022 走看看