zoukankan      html  css  js  c++  java
  • IOS NSLayoutConstraint 页面布局(通过代码添加约束)

    #import "ViewController.h"
    
    @interface ViewController ()
    @property (nonatomic, strong) UIView *blueView;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        
        
        // 2.添加两个控件到父控件上
        // 2.1添加蓝色View
        UIView *blueView = [[UIView alloc] init];
        blueView.backgroundColor = [UIColor blueColor];
    //    blueView.frame = CGRectMake(100, 100, 100, 100);
        [self.view addSubview:blueView];
        self.blueView = blueView;
        
        // 2.1添加红色View
        UIView *redView = [[UIView alloc] init];
        redView.backgroundColor = [UIColor redColor];
        [self.view addSubview:redView];
        
        // 1.禁用auturezing
    #warning 注意, 设置父控件无效
    //    self.view.translatesAutoresizingMaskIntoConstraints = NO;
        blueView.translatesAutoresizingMaskIntoConstraints = NO;
        redView.translatesAutoresizingMaskIntoConstraints = NO;
        
        // 3.添加约束
        // 3.1添加蓝色VIew距离父控件左边的距离固定为20  X
        
        /*
         Item == first item 需要设置约束的控件
         attribute == 需要设置的约束
         relatedBy == relation   等于
         toItem == second item    被参照的控件
         attribute == 需要设置的约束
         multiplier == multiplier  乘以
         constant = constant   加上
         */
       
        NSLayoutConstraint *leftCos = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
        [self.view addConstraint:leftCos];
        
        // 3.2添加蓝色VIew距离父控件右边的距离固定为20  宽度
        NSLayoutConstraint *rightCos = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
        [self.view addConstraint:rightCos];
        
        // 3.3添加蓝色VIew距离父控件顶部边的距离固定为20  Y
        NSLayoutConstraint *topCos = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:20];
        [self.view addConstraint:topCos];
        
        // 3.4添加蓝色View的高度 50  高
        NSLayoutConstraint *heightCos = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:50];
        [blueView addConstraint:heightCos];
        
        
        // 4.设置红色约束
        // 红色的高度和蓝色高度一样  高度
        NSLayoutConstraint *redHeightCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
        [self.view addConstraint:redHeightCos];
        
        // 红色的右边和蓝色的右边对齐  X
         NSLayoutConstraint *redRightCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        [self.view addConstraint:redRightCos];
        
        //  红色的顶部和蓝色的底部距离固定  Y
         NSLayoutConstraint *redTopCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20];
        [self.view addConstraint:redTopCos];
        
        // 红色的宽度等于蓝色宽度的一半  宽度
       NSLayoutConstraint *redwidthCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0];
        [self.view addConstraint:redwidthCos];
        
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"%@", NSStringFromCGRect(self.blueView.frame));
    }
    
    @end
  • 相关阅读:
    STM32时钟配置方法详解
    STM32 入门之 GPIO
    arm可以干什么
    四两拨千斤,ARM是如何运作、靠什么赚钱的
    ARM内核全解析,从ARM7,ARM9到Cortex-A7,A8,A9,A12,A15到Cortex-A53,A57
    ARM与单片机到底有啥区别
    实得打印机色带芯更换
    IE浏览器不能上传图片
    IE FANS
    win8,win10里面内置的IE浏览器网银无法输入密码
  • 原文地址:https://www.cnblogs.com/liuwj/p/6890029.html
Copyright © 2011-2022 走看看