1、要开始使用AutoLayout,请先设置要约束的view的translatesAutoresizingMaskIntoConstraints属性为NO。在xib或者sb中勾选Use Auto Layout,那么所有在xib或者sb中出现的view都已经默认将translatesAutoresizingMaskIntoConstraints设置为NO。
self.view.translatesAutoresizingMaskIntoConstraints = NO; // self.view 代表 所有你要添加约束的view
2、在使用AutoLayout布局的view中,代码中避免出现设置其frame相关属性(如center)的代码,但是可以获取其frame;
3、通过代码为xib或sb中view增加约束时,尽量避免在viewDidLoad中执行,最好放在updateViewConstraints[UIViewController]或者updateConstraints[UIView]中,记得调用[super updateViewConstraints]或者[super updateConstraints];
- (void)updateViewConstraints
{
// 在这里为你的view添加约束,请确保该view的translatesAutoresizingMaskIntoConstraints属性已设置为NO
[super updateViewConstraints];
}
如果需要在控制器中动态添加或者移除视图,在控制器中为新添加的视图增加约束,在updateViewConstrains中实现,然后调用[super updateViewConstrains];同理,在view中动态添加或者移除视图,在updateConstrains中实现,然后调用[super updateConstrains]
如果你真的写在viewDidLoad里了,那么可能会遇到这种崩溃错误
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Impossible to set up layout with view hierarchy unprepared for constraint.'
在viewDidLoad方法中,view的层级并没有装载完成,这个方法只是将所有UIView的实例加载到内存中,而没有完成层级的组装,这个时候,针对自定义的UIView子类的实例使用LayoutConstraint,参照对象是self.view, 就会产生Crash。
4.控制器在其view需要重新布局时会执行以下过程:
① 控制器的视图调整到新的尺寸 - 控制器会根据当前状态栏、导航条等其它因素的状态来调整其view的位置尺寸
② 如果没有使用autolayout,所有子视图会根据autoresizeing mask调整
③ 调用viewWillLayoutSubviews
④ 调用控制器视图的layoutSubviews,如果是使用autolayout,则会调用updateViewConstrains -> 该方法的实现会调用所有子视图的updateConstraints -> 更新完约束之后,所有视图会根据计算出来的新的布局更新位置
⑤ 调用控制器的viewDidLayoutSubviews
如果是viewControl则AutoLayout适配写在- (void)updateViewConstraints方法中修改约束;
如果是view则AutoLayout适配 则在- (void)updateConstraints方法中修改。
可以直接把视图的约束像拖控件一样拖成属性直接在下面的方法里更改即可
//遍历self.myView控件中的约束,找到NSLayoutAttributeHeight控件高度这个约束关系,然后修改
-(void)updateViewConstraints
{
[superupdateViewConstraints];
NSArray *constrains = self.myView.constraints;
for(NSLayoutConstraint *constraintin constrains){
if(constraint.firstAttribute ==NSLayoutAttributeHeight){
constraint.constant = 50.0;
}
}
}
- (void)updateViewConstraints{
[super updateViewConstraints];
[self customeBtnHeight:30.0f andNumOfBtns:4 withPadding:0.0f withTempArray:tempA];
}
/**
自定义并排按钮封装
@param height 按钮高度
@param count 按钮数量
@param padding 按钮间隔
@param tempArr 按钮数量
author: yuan
*/
- (void)customeBtnHeight:(CGFloat)height andNumOfBtns:(NSInteger)count withPadding:(CGFloat)padding withTempArray:(NSArray *)tempArr{
UIButton * tempBtn = nil;
for (NSInteger i = 0; i < count; i++) {
[topView addSubview:tempArr[i]];
if (i == 0) {
[tempArr[i] mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(topView).offset(padding);
make.height.equalTo(@(height));
make.centerY.equalTo(topView);
}];
} else if (i == count -1) {
[tempArr[i] mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(tempBtn.mas_right).offset(padding);
make.right.equalTo(topView.mas_right).offset(-padding);
make.height.equalTo(tempBtn);
make.width.equalTo(tempBtn);
make.centerY.equalTo(tempBtn);
}];
} else {
[tempArr[i] mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(tempBtn.mas_right).offset(padding);
make.centerY.equalTo(tempBtn);
make.height.equalTo(tempBtn);
make.width.equalTo(tempBtn);
}];
}
tempBtn = tempArr[i];
}
}