VFL簡介
VFL全稱是Visual Format Language(可視化格式語言)臼节,它簡化了Autolayout练湿, 通過一行字符串,你可以在水平或者垂直方向上指定多個約束, 這跟一次只能創(chuàng)建一個約束相比會節(jié)省大量的代碼量
編譯代碼寫的布局時,相關(guān)的View都需要將translatesAutoresizingMaskIntoConstraints設(shè)置為NO
blueView.translatesAutoresizingMaskIntoConstraints = NO;
VFL用法
VFL字符串語法
- H :水平方向
-
V :垂直方向
注: 不指定方向默認水平方法
- | :父視圖
- -:標準間隔(默認左右邊距8像素,上邊距20像素)
- -xx- 非標準間隔(xx像素)
- ==:寬度相等(可省略)
- <=:小于等于
- >=:大于等于
- @250:約束的優(yōu)先級童谒,取值范圍為0~1000(250、750沪羔、1000)
示例:
H:|-20-[blueView]-20-|
水平方向上饥伊,blueView距離父視圖的左右倆邊距均為20
V:|-[button(50.0)]:
垂直方向上,距離父視圖頂部標準默認間距20蔫饰,button高度為50
H:|-20-[blueView(100)]
水平方向上琅豆,blueView距離父視圖的左邊距為20,blueView的寬為100
V:[blueView(50)]-100@250-|
垂直方向上篓吁,blueView高度為50茫因,blueView 距離父視圖底部邊距為100,@250優(yōu)先級為低, 如果自動布局有沖突時, 該條約束就有可能失效
H:|-[blueView(50)]-10-[redView]-10-[yellowView(blueView)]|
水平方向上越除,blueView距離父視圖的左邊距為標準間隔(默認8像素),yellowView距離父視圖的右邊距為0外盯,redView距離blueView和yellowView左右邊距均為10摘盆,blueView和yellowView等寬
主要函數(shù)
+ (NSArray *)constraintsWithVisualFormat:(NSString *)format
options:(NSLayoutFormatOptions)opts
metrics:(NSDictionary *)metrics
views:(NSDictionary *)views
format:VFL語句
VFL字符串-
opts:約束類型
opts參數(shù)是個枚舉值,表示對齊方式
UIView *blueView = [[UIView alloc]init]; blueView.backgroundColor = [UIColor blueColor]; [self.view addSubview:blueView]; blueView.translatesAutoresizingMaskIntoConstraints = NO; UIView *redView = [[UIView alloc]init]; redView.backgroundColor = [UIColor redColor]; [self.view addSubview:redView]; redView.translatesAutoresizingMaskIntoConstraints = NO; NSNumber *width = @100.0; NSNumber *height = @100.0; NSDictionary *viewsDic = @{@"blueView":blueView,@"redView":redView}; NSDictionary *metricsDic = @{@"width":width,@"height":height}; //設(shè)置對齊方式饱苟,redView與blueView的上面和下面對齊 NSLayoutFormatOptions ops = NSLayoutFormatAlignAllTop|NSLayoutFormatAlignAllBottom; //水平約束 NSString *vfl1 = @"H:|-50-[blueView(width)]-30-[redView(width)]"; NSArray *constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:vfl1options:ops metrics:metricsDic views:viewsDic]; [self.view addConstraints:constraints1]; //垂直約束 NSString *vfl2 = @"V:|-100-[blueView(height)]"; NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:vfl2 options:ops metrics:metricsDic views:viewsDic]; [self.view addConstraints:constraints2];
-
metrics:VFL語句中用到的具體數(shù)值
metrics參數(shù)是一個字典, 里面可以存儲一些數(shù)值, 這樣存儲之后就可以在VFL字符串中調(diào)用了UIView *blueView = [[UIView alloc]init]; blueView.backgroundColor = [UIColor blueColor]; [self.view addSubview:blueView]; blueView.translatesAutoresizingMaskIntoConstraints = NO; CGFloat height = 100.0; CGFloat width = 100.0; //width代替 100.0孩擂,height代替 100.0 NSString *vfl1 = @"H:|-30-[blueView(width)]-30-|"; NSDictionary *views = @{@"blueView":blueView}; //metrics參數(shù)中做好對應(yīng)關(guān)系 NSArray *constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:vfl1 options:kNilOptions metrics:@{@"width":@(width)} views:views]; [self.view addConstraints:constraints1]; NSString *vfl2 = @"V:|-30-[blueView(height)]-30-|"; NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:vfl2 options:kNilOptions metrics:@{@"height":@(height)} views:views]; [self.view addConstraints:constraints2];
-
views:VFL語句中用到的控件
views參數(shù)是一個字典,里面可以存儲一些數(shù)值, 這樣存儲之后就可以在VFL字符串中調(diào)用了
為了方便蘋果官方提供了一個宏,傳入相關(guān)的view變量名箱熬,返回可以直接使用的dictionary
示例NSDictionaryOfVariableBindings() 示例 NSDictionaryOfVariableBindings(v1, v2, v3) 等價于 [NSDictionary dictionaryWithObjectsAndKeys:v1, @”v1”, v2, @”v2”, v3, @”v3”, nil];
UIView *blueView = [[UIView alloc]init]; blueView.backgroundColor = [UIColor blueColor]; [self.view addSubview:blueView]; blueView.translatesAutoresizingMaskIntoConstraints = NO; CGFloat height = 100.0; CGFloat width = 100.0; NSString *vfl1 = @"H:|-30-[blueView(width)]-30-|"; NSDictionary *views = NSDictionaryOfVariableBindings(blueView); //NSDictionary *views = @{@"blueView":blueView}; //同上 NSArray *constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:vfl1 options:kNilOptions metrics:@{@"width":@(width)} views:views]; [self.view addConstraints:constraints1]; NSString *vfl2 = @"V:|-30-[blueView(height)]-30-|"; NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:vfl2 options:kNilOptions metrics:@{@"height":@(height)} views:views]; [self.view addConstraints:constraints2];
VFL缺點
VFL可以滿足大部分布局需求类垦,不能設(shè)置一個視圖自身的寬高比,也不能劇中對齊父視圖城须。為了做到這些蚤认,你需要創(chuàng)建一些獨特的約束