VFL-visual format language - 可视化格式语言

//一个控件的情况

UIView *blueView = [UIView new];

blueView.backgroundColor = [UIColor blueColor];

blueView.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addSubview:blueView];

NSString *vfl = @"H:|-30-[blueView]-30-|";

NSDictionary *views = NSDictionaryOfVariableBindings(blueView);

NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:vfl options:kNilOptions metrics:nil views:views];

[self.view addConstraints:constraints];

NSString *vfl2 = @"V:|-20-[blueView(40)]";

NSDictionary *views2 = NSDictionaryOfVariableBindings(blueView);

NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:vfl2 options:kNilOptions metrics:nil views:views2];

[self.view addConstraints:constraints2];

//两个控件的情况

UIView *blueview = [UIView new];

blueview.backgroundColor = [UIColor blueColor];

blueview.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addSubview:blueview];

UIView *redview = [UIView new];

redview.backgroundColor = [UIColor redColor];

redview.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addSubview:redview];

//水平方向

NSNumber *margin = @20;

NSString *vfl12 = @"H:|-margin-[blueview]-margin-[redview(==blueview)]-margin-|";

NSDictionary *views1 = NSDictionaryOfVariableBindings(blueview,redview);

NSDictionary *metrics = NSDictionaryOfVariableBindings(margin);

NSArray *constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:vfl12 options:kNilOptions metrics:metrics views:views1];

[self.view addConstraints:constraints1];

//竖直方向

NSNumber *height = @40;

NSString *vfl3 = @"V:[blueview(height)]-margin-|";

NSDictionary *views3 = NSDictionaryOfVariableBindings(blueview,redview);

NSDictionary *metrics3 = NSDictionaryOfVariableBindings(margin,height);

NSArray *constraints3 = [NSLayoutConstraint constraintsWithVisualFormat:vfl3 options:kNilOptions metrics:metrics3 views:views3];

[self.view addConstraints:constraints3];

NSLayoutConstraint *constraint4 = [NSLayoutConstraint constraintWithItem:redview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueview attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];

NSLayoutConstraint *constraint5 = [NSLayoutConstraint constraintWithItem:redview attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:blueview attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];

[self.view addConstraint:constraint4];

[self.view addConstraint:constraint5];

你可能感兴趣的:(VFL-visual format language - 可视化格式语言)