+ (id)constraintWithItem:attribute:relatedBy:toItem: attribute:multiplier:constant:
参数说明:
WithItem:要约束的对象
attribute:对象的布局属性
relatedBy:布局关系
toItem:参照对象
attribute:参照对象的布局属性
multiplier:乘数
constant:常数
自动布局的核心公式
Object1.property1=(object2.property2*multiplier)+constant value
————————————————————————————————————————
-(void)addConstraint:(NSLayoutConstraint *)constraint
-setNeedsUpdateConstraints
-layoutIfNeeded
[button setTranslatesAutoresizingMaskIntoConstraints:NO];
------------------------------------------------------------------------------
// 2.1 水平方向的约束
NSLayoutConstraint *constraintX = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraintX];
// 2.2 垂直方向的约束
NSLayoutConstraint *constraintY = [NSLayoutConstraint
=========================================
测试演练5——VFL演练(1)
// 1. 创建三个按钮:top、center、bottom
// 2. 三个按钮宽度为100点
// 3. 三个按钮距离屏幕右侧20点
// 4. bottom按钮距离屏幕底部20点,每个按钮纵向间距为20点
NSDictionary *viewDict = NSDictionaryOfVariableBindings(top, center, bottom);
NSArray *consh1 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[top(100)]-20-|" options:0 metrics:nil views:viewDict];
……
NSArray *consv = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[top(40)]-20-[center(40)]-20-[bottom(40)]-20-|" options:0 metrics:nil views:viewDict];
……