学习笔记
由于好奇今天我就捣鼓了一下午,感觉VFL挺直观的。废话不多说了,贴代码。
- (void)viewDidLoad {
[superviewDidLoad];
[selfsetupViews];
int index = [self.numberintValue];
switch (index) {
case 0:{
[self.viewaddSubview:cyanView];
[self.viewaddSubview:redView];
[self.viewaddSubview:orangeView];
[selfexample_1];
}break;
case 1:{
[self.viewaddSubview:cyanView];
[cyanViewaddSubview:redView];
[redViewaddSubview:orangeView];
[redViewaddSubview:view1];
[redViewaddSubview:button];
[selfexample_2];
}break;
case 2:
[self.viewaddSubview:cyanView];
[self.viewaddSubview:redView];
[self.viewaddSubview:orangeView];
[self.viewaddSubview:button];
[self.viewaddSubview:view1];
[selfexample_3];
break;
case 3:
[self.viewaddSubview:cyanView];
[self.viewaddSubview:redView];
[self.viewaddSubview:orangeView];
[self.viewaddSubview:button];
[self.viewaddSubview:view1];
[self.viewaddSubview:view2];
[selfexample_4];
break;
default:
break;
}
}
- (void)setupViews {
cyanView = [[UIViewalloc] init];
[cyanViewsetTranslatesAutoresizingMaskIntoConstraints:NO];
cyanView.backgroundColor = [UIColorcolorWithRed:168 /255.0f green:217 /255.0f blue:1alpha:1];
redView = [[UIViewalloc] init];
[redViewsetTranslatesAutoresizingMaskIntoConstraints:NO];
redView.backgroundColor = [UIColorcolorWithRed:1green:189 /255.0f blue:197 /255.0f alpha:1];
orangeView = [[UIViewalloc] init];
orangeView.translatesAutoresizingMaskIntoConstraints =NO;
orangeView.backgroundColor = [UIColorcolorWithRed:1green:232 /255.0f blue:197 /255.0f alpha:1];
button = [UIButtonbuttonWithType:UIButtonTypeCustom];
[buttonsetTitle:@"back"forState:UIControlStateNormal];
button.translatesAutoresizingMaskIntoConstraints =NO;
button.backgroundColor = [UIColorcolorWithRed:178 /255.0f green:243 /255.0f blue:249 /255.0f alpha:1];
[buttonaddTarget:selfaction:@selector(btnAction)forControlEvents:UIControlEventTouchUpInside];
view1 = [[UIViewalloc] init];
view1.translatesAutoresizingMaskIntoConstraints =NO;
view1.backgroundColor = [UIColorcolorWithRed:203 /255.0f green:1blue:173 /255.0f alpha:1];
view2 = [[UIViewalloc] init];
view2.translatesAutoresizingMaskIntoConstraints =NO;
view2.backgroundColor = [UIColorcolorWithRed:168 /255.0f green:122 /255.0f blue:173 /255.0f alpha:1];
//通过宏映射成[NSDictionary dictionaryWithObjectsAndKeys:v1, @"v1", v2, @"v2", nil];
dic =NSDictionaryOfVariableBindings(cyanView,redView,orangeView,view1,view2,button);
}
- (void)example_1 {
// redView在父视图的上边缘,距离15
[selfsuperView:self.view :@"V:|-15-[redView]" :0];
// redView在父视图的左边缘,距离15,redView的右侧cyanView相距15,cyanView在父视图右边缘,距离15。一样的top约束
[selfsuperView:self.view :@"H:|-15-[redView]-15-[cyanView]-15-|" :NSLayoutFormatAlignAllTop];
// cyanView的高度等于redView的高度
[selfsuperView:self.view :@"V:[cyanView(==redView)]" :0];
// cyanView的宽度等于redView的宽度
[selfsuperView:self.view :@"H:[cyanView(==redView)]" :0];
// redView和orange垂直距离为15
[selfsuperView:self.view :@"V:[redView]-15-[orangeView]" :0];
// orangeView处在父视图的左右边缘内距离15
[selfsuperView:self.view :@"H:|-15-[orangeView]-15-|" :0];
// orangeView下边和父视图的距离为15
[selfsuperView:self.view :@"V:[orangeView]-15-|" :0];
// orangeView宽度等于redView的宽度
[selfsuperView:self.view :@"V:[orangeView(==redView)]" :0];
}
- (void)example_2 {
[selfsuperView:self.view :@"H:|-20-[cyanView]-20-|" :0];
[selfsuperView:self.view :@"V:|-20-[cyanView]-20-|" :0];
[selfsuperView:cyanView :@"H:|-20-[redView]-20-|" :0];
[selfsuperView:cyanView :@"V:|-20-[redView]-20-|" :0];
[selfsuperView:redView :@"H:|-20-[orangeView]-20-|" :0];
[selfsuperView:redView :@"V:|-20-[orangeView]" :0];
[selfsuperView:redView :@"H:[button(==orangeView)]" :0];
[selfsuperView:redView :@"V:[button(==orangeView)]" :0];
[selfsuperView:redView :@"H:[view1(orangeView)]" :0];
[selfsuperView:redView :@"V:[view1(orangeView)]" :0];
[selfsuperView:redView :@"V:[view1]-20-|" :0];
// orangeView和button和View1之间间隔为20并垂直齐平
[selfsuperView:redView :@"V:[orangeView]-20-[button]-20-[view1]" :NSLayoutFormatAlignAllCenterX];
}
- (void)example_3 {
[selfsuperView:self.view :@"H:|-20-[cyanView]-20-|" :0];
[selfsuperView:self.view :@"H:[redView(cyanView)]" :0];
[selfsuperView:self.view :@"V:[redView(cyanView)]" :0];
[selfsuperView:self.view :@"H:[button(cyanView)]" :0];
[selfsuperView:self.view :@"V:[button(cyanView)]" :0];
[selfsuperView:self.view :@"H:[orangeView(cyanView)]" :0];
[selfsuperView:self.view :@"V:[orangeView(cyanView)]" :0];
[selfsuperView:self.view :@"H:[view1(cyanView)]" :0];
[selfsuperView:self.view :@"V:[view1(cyanView)]" :0];
// cyanView和父视图左边缘的距离为20,其他之间的间隔为15并垂直齐平
[selfsuperView:self.view :@"V:|-20-[cyanView]-15-[redView]-15-[button]-15-[orangeView]-15-[view1]-15-|" :NSLayoutFormatAlignAllCenterX];
}
- (void)example_4 {
// cyanView和父视图左边缘的距离为20,与redView的间距为15,redView和父视图右边缘的距离为20并水平齐平
[selfsuperView:self.view :@"H:|-20-[cyanView]-15-[redView]-20-|" :NSLayoutFormatAlignAllCenterY];
[selfsuperView:self.view :@"V:|-20-[cyanView]-15-[button]-15-[view1]-20-|" :NSLayoutFormatAlignAllCenterX];
[selfsuperView:self.view :@"V:|-20-[redView]-15-[orangeView]-15-[view2]-20-|" :NSLayoutFormatAlignAllCenterX];
[selfsuperView:self.view :@"H:[redView(cyanView)]" :0];
[selfsuperView:self.view :@"V:[redView(cyanView)]" :0];
[selfsuperView:self.view :@"H:[button(cyanView)]" :0];
[selfsuperView:self.view :@"V:[button(cyanView)]" :0];
[selfsuperView:self.view :@"H:[orangeView(cyanView)]" :0];
[selfsuperView:self.view :@"V:[orangeView(cyanView)]" :0];
[selfsuperView:self.view :@"H:[view1(cyanView)]" :0];
[selfsuperView:self.view :@"V:[view1(cyanView)]" :0];
[selfsuperView:self.view :@"H:[view2(cyanView)]" :0];
[selfsuperView:self.view :@"V:[view2(cyanView)]" :0];
}
- (void)superView :(UIView *)superView :(NSString *)format :(NSLayoutFormatOptions)options {
[superView addConstraints:
[NSLayoutConstraintconstraintsWithVisualFormat:format
options:options
metrics:nil
views:dic]];
}
LCY_VFLAutoLayout 文件下载
好记性,不如烂笔头。