博客> VFL的简单用法
VFL的简单用法
2017-12-10 16:11 评论:0 阅读:82 进击的可乐SAMA

1.使用一个视图


UIView * blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
//默认为YES,设为NO
blueView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:blueView];

//写出VFL语句 设置约束
NSString * hVFL = @"H:|-100-[blueView]-100-|";
//注:H表示为横向约束,V表示为纵向约束,|表示为左右(上下)边界.[blueView(x)]x表示为长宽。

//将语句放入数组中
NSArray * hCons = [NSLayoutConstraint constraintsWithVisualFormat:hVFL options:0 metrics:nil views:@{@"blueView":blueView}];
/*
第一个参数:填写VFL语句
第二个参数:默认类型为0,有左右上下四个对其方式,枚举对齐方式
第三个参数:填写nil
第四个参数:VFL语句用到几个视图,填写几个,用字典存起来
*/

//父视图添加语句
[self.view addConstraints:hCons];

NSString * vVFL = @"V:|-300-[blueView(100)]";
NSArray * vCons = [NSLayoutConstraint constraintsWithVisualFormat:vVFL options:0 metrics:nil views:@{@"blueView":blueView}];
[self.view addConstraints:vCons];

2.多个视图


UIView * redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
redView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:redView];

UIView * blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
blueView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:blueView];

NSString * hVFL = @"H:|-50-[redView(100)]-75-[blueView(100)]-50-|";
NSArray * hCons = [NSLayoutConstraint constraintsWithVisualFormat:hVFL options:0 metrics:nil views:@{@"redView":redView, @"blueView":blueView}];
[self.view addConstraints:hCons];

NSString * redVVFL = @"V:|-100-[redView(50)]";
NSArray * redVCons = [NSLayoutConstraint constraintsWithVisualFormat:redVVFL options:0 metrics:nil views:@{@"redView":redView}];
[self.view addConstraints:redVCons];

NSString * blueVVFL = @"V:|-150-[blueView(50)]";
NSArray * blueVCons = [NSLayoutConstraint constraintsWithVisualFormat:blueVVFL options:0 metrics:nil views:@{@"blueView":blueView}];
[self.view addConstraints:blueVCons];
收藏
0
sina weixin mail 回到顶部