用H:来定义水平方向的约束,用V:来定义垂直方向的约束
下面我们来看几个例子
H:|-100-[btn]-100-| btn置于父控件的中间左右留空100
H:|-(<=100)-[btn(>=50)]-(<=100)-| @_@ 自己想象下这会是什么意思
H:|-[btn(>=100,<=200)] 你很聪明的,我知道,所以我也不想多说什么了。
下面是我写的一个小例子
#pragma mark 测试 Visual Format Language
-(void)testVisualFormatLanguage
{
//第一个控件
UITextField * textField = [[UITextField alloc] init];
textField.translatesAutoresizingMaskIntoConstraints = NO;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.placeholder = @"Email Address";
[self.view addSubview:textField];
NSString *const kTextFieldHorizontal = @"H:|-[textField]-|";
NSString *const kTextFieldVertical = @"V:|-[textField]";
NSMutableArray * textFieldConstraints = [[NSMutableArray alloc] init];
NSDictionary *textFieldDic = NSDictionaryOfVariableBindings(textField);
[textFieldConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kTextFieldHorizontal options:0 metrics:nil views:textFieldDic]];
[textFieldConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kTextFieldVertical options:0 metrics:nil views:textFieldDic]];
//第二个控件
UITextField * textField1 = [[UITextField alloc] init];
textField1.translatesAutoresizingMaskIntoConstraints = NO;
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.placeholder = @"Password";
[self.view addSubview:textField1];
NSString *const kTextField1Horizontal = @"H:|-[textField1]-|";
NSString *const kTextField1Vertical = @"V:[textField]-[textField1]";
NSMutableArray * textField1Constraints = [[NSMutableArray alloc] init];
NSDictionary *textField1Dic = NSDictionaryOfVariableBindings(textField,textField1);
[textField1Constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kTextField1Horizontal options:0 metrics:nil views:textField1Dic]];
[textField1Constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kTextField1Vertical options:0 metrics:nil views:textField1Dic]];
//第三个控件
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.translatesAutoresizingMaskIntoConstraints = NO;
[btn setTitle:@"OK" forState:UIControlStateNormal];
[self.view addSubview:btn];
NSString *const kButtonVertical = @"V:[textField1]-[btn]";
NSMutableArray *btnConstraints = [NSMutableArray array];
NSDictionary * btnDic = NSDictionaryOfVariableBindings(btn,textField1);
[btnConstraints addObject:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:btn.superview attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
[btnConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kButtonVertical options:0 metrics:nil views:btnDic]];
//把这些Contraints 加进来
NSMutableArray * allContraints = [NSMutableArray array];
[allContraints addObjectsFromArray:textFieldConstraints];
[allContraints addObjectsFromArray:textField1Constraints];
[allContraints addObjectsFromArray:btnConstraints];
[self.view addConstraints:allContraints];
}
放到你的代码中,并在viewDidLoad那里调用一下。你就能在界面上看到这三个控件了,别忘了旋转下,换个设备试试效果哦。