- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
[button1 setTitle:@"Button1" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor grayColor];
[self.view addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
[button2 setTitle:@"Button2" forState:UIControlStateNormal];
button2.backgroundColor = [UIColor grayColor];
[self.view addSubview:button2];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
[button3 setTitle:@"Button3" forState:UIControlStateNormal];
button3.backgroundColor = [UIColor grayColor];
[self.view addSubview:button3];
//1. 关掉Autoresizing
button1.translatesAutoresizingMaskIntoConstraints = NO;
button2.translatesAutoresizingMaskIntoConstraints = NO;
button3.translatesAutoresizingMaskIntoConstraints = NO;
//2. VFL
NSString *vfl = @"|-left-[button1]-10-[button2(==button1)]-10-[button3(==button1)]-right-|";
//3. 创建约束
//NSDictionary *views = @{@"button1" : button1, @"button2" : button2, @"button3" : button3};
NSDictionary *views = NSDictionaryOfVariableBindings(button1, button2, button3);
NSDictionary *metricss = @{@"left" : @10, @"right" : @10};
NSArray *constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:vfl options:NSLayoutFormatAlignAllCenterY metrics:metricss views:views];
//4. 将约束加入到父视图
[self.view addConstraints:constraints1];
// V:表示垂直高度
vfl = @"V:|-200-[button1]";
NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:vfl options:0 metrics:nil views:views];
[self.view addConstraints:constraints2];
}