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];
        
    }
    

      

  • 相关阅读:
    linux 彻底删除文件及 find命令permission refused问题解决
    ubuntu系统中dpkg lock问题分析及解决
    ubuntu server18.04 更换默认源为阿里源-加速
    docker安装与卸载( liunx )
    ubuntu下dpkg lock问题
    docker pull报x509问题及docker启动失败问题解决
    windows 常用命令行操作
    uwsgi运行django应用是报错no app loaded. going in full dynamic mode
    internal server error原因及解决
    docker-compose启动容器后执行脚本或命令不退出 | 运行内部程序
  • 原文地址:https://www.cnblogs.com/mgbert/p/4067178.html
Copyright © 2011-2022 走看看