iOS动态管理AutoLayout的约束NSLayoutConstraint

时间:2023-12-25 22:56:25

除了使用Storyboard之外,也可以使用使用代码的方式,动态的向指定的UIView,添加约束。

例如有两个UILabel:someLabel,otherLabel

首先用代码实例化,两个控件

self.someLabel = [[UILabel alloc]initWithFrame:CGRectZero];
self.someLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.inputContainerView]; self.otherLabel = [[UILabel alloc]initWithFrame:CGRectZero];
self.otherLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.inputContainerView];

两点很重要:

  1. translatesAutoresizingMaskIntoConstraints = NO  否则布局就会混乱
  2. someLabel和otherLabel必须有superView,否则就会出现出现警告

The view hierarchy is not prepared for the constraint

When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled.

运行时候Crash

Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view.  Does the constraint reference something from outside the subtree of the view?  That's illegal

例如新建了一个UIView的Category

其中一个方法

-(NSLayoutConstraint*)verticalDistanceTop:(UIView*)topView constant:(CGFloat)constant
{
NSLayoutConstraint*topConstrant = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:topView attribute:NSLayoutAttributeBottom multiplier: constant:constant];
return topConstrant;
}

返回一个约束,保证self具体topView的底部距离为contant

约束的构造函数 :constraintWithItem,注意计算公式:

view1.attr1 = view2.attr2 * multiplier + constant
/* Create constraints explicitly.  Constraints are of the form "view1.attr1 = view2.attr2 * multiplier + constant"
If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
*/
+(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;

生成约束后,使其生效的方法

 NSLayoutConstraint*topConstraint = [self.someLabel verticalDistanceTop:self.otherLabel constant:];
topConstraint.active = YES;