1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 12 [self fun5]; 13 14 } 15 16 -(void)fun1{ 17 //注意使用VFL,不用設置視圖的frame 18 UIView *view = [[UIView alloc] init]; 19 view.backgroundColor = [UIColor brownColor]; 20 [self.view addSubview:view]; 21 22 view.translatesAutoresizingMaskIntoConstraints = NO;//注意,要想使約束條件起做用,必須將此屬性設置爲no 23 24 /* 25 VFL相關的標識符 26 H:表明水平方向 27 V:表明垂直方向 28 |表明父視圖 29 - 表明距離 例(H:|-20-)表示距離父視圖左側20像素 30 []表明子視圖相關屬性 31 */ 32 33 //對子視圖View進行約束設置,format表示要設置的約束語句,在本句中意思爲:view距離父視圖左側30像素,距離父視圖右側20像素(若是沒設置具體的數字,且寫了-) 34 //options表明對其方式,默認寫0就行 35 //metrics 這裏是一個字典,當在format中使用了動態數據好比上現這句:@"H:|-[button(==width)]-|",表示這個button的寬度爲width,那麼這個參數去哪裏找呢?就是在這個字典裏面找到key對就的值,若是沒有找到這個值,app會crash 36 //views 表明約束條件中所涉及的全部視圖(父視圖除外) 37 NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-30-[view]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]; 38 39 NSArray *arr1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[view]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]; 40 41 [self.view addConstraints:arr]; 42 [self.view addConstraints:arr1]; 43 44 } 45 46 -(void)fun2{ 47 UIView *view = [[UIView alloc] init]; 48 view.backgroundColor = [UIColor grayColor]; 49 [self.view addSubview:view]; 50 51 view.translatesAutoresizingMaskIntoConstraints = NO; 52 //[view(>=200)] 表示給view設置尺寸,此時小括號裏的關係運算符只能是 == <= >=,注意,改尺寸的值不要與約束衝突,==:約束能夠不寫,簡寫成H:[view(==200)] >=:當左右距離只設置了一個或者都未設置,視圖的尺寸爲最小值,若是兩個都設置時,view 的尺寸爲屏幕寬度減去左右距離,此時要保證減後的值>=尺寸最小值 53 54 NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[view(>=200)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]; 55 56 NSArray *arr1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[view(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]; 57 [self.view addConstraints:arr1]; 58 [self.view addConstraints:arr]; 59 60 } 61 62 -(void)fun3{ 63 UIView *view = [[UIView alloc] init]; 64 view.backgroundColor = [UIColor grayColor]; 65 [self.view addSubview:view]; 66 67 view.translatesAutoresizingMaskIntoConstraints = NO; 68 69 CGFloat width = 200; 70 71 //當咱們在設置約束條件時,距離父視圖上下左右的像素值以及view的尺寸不是固定的數字(用某個字符串代替),這時咱們須要給metrics設置一個字典,該字典的key必須是約束語句中使用的字符串,對應的value能夠自行設置 72 NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-left-[view(==width)]" options:0 metrics:@{@"width":[NSNumber numberWithFloat:width],@"left":@30} views:NSDictionaryOfVariableBindings(view)]; 73 74 NSArray *arr1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[view(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]; 75 [self.view addConstraints:arr1]; 76 [self.view addConstraints:arr]; 77 78 } 79 80 -(void)fun4{ 81 UIView *view = [[UIView alloc] init]; 82 view.backgroundColor = [UIColor grayColor]; 83 [self.view addSubview:view]; 84 85 view.translatesAutoresizingMaskIntoConstraints = NO; 86 87 CGFloat width = 200; 88 89 //當咱們在設置約束條件時,距離父視圖上下左右的像素值以及view的尺寸不是固定的數字(用某個字符串代替),這時咱們須要給metrics設置一個字典,該字典的key必須是約束語句中使用的字符串,對應的value能夠自行設置 90 NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-left-[view(==width)]" options:0 metrics:@{@"width":[NSNumber numberWithFloat:width],@"left":@30} views:NSDictionaryOfVariableBindings(view)]; 91 92 NSArray *arr1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[view(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]; 93 [self.view addConstraints:arr1]; 94 [self.view addConstraints:arr]; 95 96 97 UIView *view1 = [[UIView alloc] init]; 98 view1.backgroundColor = [UIColor purpleColor]; 99 [self.view addSubview:view1]; 100 101 view1.translatesAutoresizingMaskIntoConstraints = NO; 102 103 //[view1(view)] 表明view1和view某個方向尺寸相同 104 NSArray *arr2 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[view1(view)]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view1,view)]; 105 106 //V:[view]-[view1]-20-|:子視圖view1垂直方向距離平級子視圖view 8像素(默認) 107 NSArray *arr3 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[view]-[view1]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view,view1)]; 108 [self.view addConstraints:arr2]; 109 [self.view addConstraints:arr3]; 110 111 112 } 113 114 //用VFL實現label自適應 115 -(void)fun5{ 116 UILabel *label = [[UILabel alloc] init]; 117 label.backgroundColor = [UIColor grayColor]; 118 label.numberOfLines = 0; 119 label.lineBreakMode = NSLineBreakByCharWrapping; 120 label.text = @"hiwehfdiuwehdiowqhdoiwehjdoiuwehdiouwhediuwheioudhewiuodhwehoihiuhiuhiuhiluhiuhiuhiuhihuhuuhhuh"; 121 [self.view addSubview:label]; 122 label.translatesAutoresizingMaskIntoConstraints = NO; 123 124 NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]; 125 126 NSArray *arr1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[label]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]; 127 128 [self.view addConstraints:arr]; 129 [self.view addConstraints:arr1]; 130 } 131 132 - (void)didReceiveMemoryWarning { 133 [super didReceiveMemoryWarning]; 134 // Dispose of any resources that can be recreated. 135 } 136 137 @end