1 -(void)fun1{
2 //注意使用VFL,不用设置视图的frame 3 UIView *view = [[UIView alloc] init]; 4 view.backgroundColor = [UIColor brownColor]; 5 [self.view addSubview:view]; 6 7 view.translatesAutoresizingMaskIntoConstraints = NO;//注意,要想使约束条件起作用,必须将此属性设置为no 8 9 /* 10 VFL相关的标识符 11 H:代表水平方向 12 V:代表垂直方向 13 |代表父视图 14 - 代表距离 例(H:|-20-)表示距离父视图左侧20像素 15 []代表子视图相关属性 16 */ 17 18 //对子视图View进行约束设置,format表示要设置的约束语句,在本句中意思为:view距离父视图左侧30像素,距离父视图右侧20像素(如果没设置具体的数字,且写了-) 19 //options代表对其方式,默认写0就行 20 //metrics 这里是一个字典,当在format中使用了动态数据比如上现这句:@"H:|-[button(==width)]-|",表示这个button的宽度为width,那么这个参数去哪里找呢?就是在这个字典里面找到key对应的值,如果没有找到这个值,app会crash 21 //views 代表约束条件中所涉及的所有视图(父视图除外) 22 NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-30-[view]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]; 23 /*这里用到了一个系统宏定义,NSDictionaryOfVariableBindings(),其作用是生成一个词典,key的名字和对象的标识符相同,以上述为例,生成的词典形式就是{"self.view":self.view,@"_view3":_view3,...},这个词典应当包含需要自动布局的父视图和所有的子视图, 24 */ 25 NSArray *arr1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[view]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]; 26 27 [self.view addConstraints:arr]; 28 [self.view addConstraints:arr1]; 29 30 } 31 32 -(void)fun2{ 33 UIView *view = [[UIView alloc] init]; 34 view.backgroundColor = [UIColor grayColor]; 35 [self.view addSubview:view]; 36 37 view.translatesAutoresizingMaskIntoConstraints = NO; 38 //[view(>=200)] 表示给view设置尺寸,此时小括号里的关系运算符只能是 == <= >=,注意,改尺寸的值不要与约束冲突,==:约束可以不写,简写成H:[view(==200)] >=:当左右距离只设置了一个或者都未设置,视图的尺寸为最小值,如果两个都设置时,view 的尺寸为屏幕宽度减去左右距离,此时要保证减后的值>=尺寸最小值 39 40 NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[view(>=200)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]; 41 42 NSArray *arr1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[view(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]; 43 [self.view addConstraints:arr1]; 44 [self.view addConstraints:arr]; 45 46 } 47 48 -(void)fun3{ 49 UIView *view = [[UIView alloc] init]; 50 view.backgroundColor = [UIColor grayColor]; 51 [self.view addSubview:view]; 52 53 view.translatesAutoresizingMaskIntoConstraints = NO; 54 55 CGFloat width = 200; 56 57 //当我们在设置约束条件时,距离父视图上下左右的像素值以及view的尺寸不是固定的数字(用某个字符串代替),这时我们需要给metrics设置一个字典,该字典的key必须是约束语句中使用的字符串,对应的value可以自行设置 58 NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-left-[view(==width)]" options:0 metrics:@{@"width":[NSNumber numberWithFloat:width],@"left":@30} views:NSDictionaryOfVariableBindings(view)]; 59 60 NSArray *arr1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[view(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]; 61 [self.view addConstraints:arr1]; 62 [self.view addConstraints:arr]; 63 64 } 65 66 -(void)fun4{ 67 UIView *view = [[UIView alloc] init]; 68 view.backgroundColor = [UIColor grayColor]; 69 [self.view addSubview:view]; 70 71 view.translatesAutoresizingMaskIntoConstraints = NO; 72 73 CGFloat width = 200; 74 75 //当我们在设置约束条件时,距离父视图上下左右的像素值以及view的尺寸不是固定的数字(用某个字符串代替),这时我们需要给metrics设置一个字典,该字典的key必须是约束语句中使用的字符串,对应的value可以自行设置 76 NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-left-[view(==width)]" options:0 metrics:@{@"width":[NSNumber numberWithFloat:width],@"left":@30} views:NSDictionaryOfVariableBindings(view)]; 77 78 NSArray *arr1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[view(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]; 79 [self.view addConstraints:arr1]; 80 [self.view addConstraints:arr]; 81 82 83 UIView *view1 = [[UIView alloc] init]; 84 view1.backgroundColor = [UIColor purpleColor]; 85 [self.view addSubview:view1]; 86 87 view1.translatesAutoresizingMaskIntoConstraints = NO; 88 89 //[view1(view)] 代表view1和view某个方向尺寸相同 90 NSArray *arr2 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[view1(view)]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view1,view)]; 91 92 //V:[view]-[view1]-20-|:子视图view1垂直方向距离平级子视图view 8像素(默认) 93 NSArray *arr3 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[view]-[view1]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view,view1)]; 94 [self.view addConstraints:arr2]; 95 [self.view addConstraints:arr3]; 96 97 98 } 99 100 //用VFL实现label自适应 101 -(void)fun5{ 102 UILabel *label = [[UILabel alloc] init]; 103 label.backgroundColor = [UIColor grayColor]; 104 label.numberOfLines = 0; 105 label.lineBreakMode = NSLineBreakByCharWrapping; 106 label.text = @"hiwehfdiuwehdiowqhdoiwehjdoiuwehdiouwhediuwheioudhewiuodhwehoihiuhiuhiuhiluhiuhiuhiuhihuhuuhhuh"; 107 [self.view addSubview:label]; 108 label.translatesAutoresizingMaskIntoConstraints = NO; 109 110 NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]; 111 112 NSArray *arr1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[label]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]; 113 114 [self.view addConstraints:arr]; 115 [self.view addConstraints:arr1]; 116 }
162