标签:
1 // 2 // ViewController.m 3 // D01-VFL语言使用(了解) 4 // 5 // Created by apple on 15/8/8. 6 // Copyright (c) 2015年 apple. All rights reserved. 7 // 8 9 10 /** 11 1.添加一个blueView和redView在控制器的View中 12 2.约束blueView的左边,右边,上边,距离控制器View的距离都为20; 13 3.约束blueView的高度为固定50; 14 4.约束redView和blueView右对齐; 15 5.约束redView的顶部距离blueView的底部为20的间距; 16 6.约束redView的宽度为blueView宽度的一半; 17 */ 18 #import "ViewController.h" 19 20 @interface ViewController () 21 22 @end 23 24 @implementation ViewController 25 26 - (void)viewDidLoad { 27 [super viewDidLoad]; 28 29 UIView *blueView = [[UIView alloc] init]; 30 blueView.backgroundColor = [UIColor blueColor]; 31 [self.view addSubview:blueView]; 32 33 34 UIView *redView = [[UIView alloc] init]; 35 redView.backgroundColor = [UIColor redColor]; 36 [self.view addSubview:redView]; 37 38 // 禁用Autoresizing功能 39 blueView.translatesAutoresizingMaskIntoConstraints = NO; 40 redView.translatesAutoresizingMaskIntoConstraints = NO; 41 /** 42 43 NSLayoutFormatAlignAllLeft = (1 << NSLayoutAttributeLeft), 44 NSLayoutFormatAlignAllRight = (1 << NSLayoutAttributeRight), 45 NSLayoutFormatAlignAllTop = (1 << NSLayoutAttributeTop), 46 NSLayoutFormatAlignAllBottom = (1 << NSLayoutAttributeBottom), 47 NSLayoutFormatAlignAllLeading = (1 << NSLayoutAttributeLeading), 48 NSLayoutFormatAlignAllTrailing = (1 << NSLayoutAttributeTrailing), 49 NSLayoutFormatAlignAllCenterX = (1 << NSLayoutAttributeCenterX), 50 NSLayoutFormatAlignAllCenterY = (1 << NSLayoutAttributeCenterY), 51 NSLayoutFormatAlignAllBaseline = (1 << NSLayoutAttributeBaseline), 52 NSLayoutFormatAlignAllLastBaseline = NSLayoutFormatAlignAllBaseline, 53 NSLayoutFormatAlignAllFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0) = (1 << NSLayoutAttributeFirstBaseline), 54 55 NSLayoutFormatAlignmentMask = 0xFFFF, 56 */ 57 58 // 添加水平方向blueView的约束 59 NSArray *HBlueViewConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[blueView]-20-|" options:NSLayoutFormatAlignAllBottom metrics:nil views:@{@"blueView" : blueView}]; 60 [self.view addConstraints:HBlueViewConstraint]; 61 62 // 添加垂直方向的约束 63 NSArray *VConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[blueView(50)]-20-[redView(==blueView)]" options:NSLayoutFormatAlignAllRight metrics:nil views:@{@"blueView" : blueView, @"redView" : redView}]; 64 [self.view addConstraints:VConstraint]; 65 66 #warning mark - VFL不支持运算符 67 // 添加redView水平方法的约束 68 // NSArray *HRedViewConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[redView(==blueView)]" options:NSLayoutFormatAlignAllRight metrics:nil views:@{@"blueView" : blueView, @"redView" : redView}]; 69 // [self.view addConstraints:HRedViewConstraint]; 70 71 // 添加redView宽度等于blueView的宽度的一半 72 NSLayoutConstraint *redViewWidth = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0]; 73 [self.view addConstraint:redViewWidth]; 74 } 75 76 @end
标签:
原文地址:http://www.cnblogs.com/DoNib-Coding/p/5014524.html