///与下面约束对象属性截图相对应
//使用Auto Layout约束,禁止将Autoresizing Mask转换为约束 [self.funcView setTranslatesAutoresizingMaskIntoConstraints:NO]; /** *@1.constraintWithItem:你想要约束的视图 *@2.attribute:约束的属性(枚举值,可使用com键查看) *@3.relatedBy:与约束的父视图的关系(枚举值) *@4.toItem:约束对象的父视图 *@5.attribute:约束的属性(枚举值,可使用com键查看),一般情况下与第二个参数写一样的 *@6.multiplier:相对于父视图的约束比例 *@7.constant:约束间隔 */ //与约束视图与父视图宽度一致 NSLayoutConstraint *w = [NSLayoutConstraint constraintWithItem:_funcView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_fview attribute:NSLayoutAttributeWidth multiplier:1 constant:0]; //与约束视图与父视图高度一致 NSLayoutConstraint *h = [NSLayoutConstraint constraintWithItem:_funcView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_fview attribute:NSLayoutAttributeHeight multiplier:1 constant:0]; //与约束视图与父视图center.x一致 NSLayoutConstraint *x = [NSLayoutConstraint constraintWithItem:_funcView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:_fview attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]; //与约束视图与父视图center.y一致 NSLayoutConstraint *y = [NSLayoutConstraint constraintWithItem:_funcView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_fview attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]; //把约束添加到父视图上 NSArray *array = @[w,h,x,y]; [_fview addConstraints:array];