iOS开发 屏幕适配(AutoLayout)

2021-12-30  本文已影响0人  Leoeoo

注意事项

1.translatesAutoresizingMaskIntoConstraints

在使用AutoLayout时,必须先把当前视图的translatesAutoresizingMaskIntoConstraints设置为NO。如不设置,可能会照成约束冲突。

如果视图是通过xib创建的,可以在xib中设置Layoutinferred(Constraints):

image.png

或通过代码设置:

view.translatesAutoresizingMaskIntoConstraints = NO;

2. setNeedsLayout、layoutIfNeeded、setNeedsUpdateConstraints、updateConstraintsIfNeeded关系

-setNeedsLayout方法: 标记为需要重新布局,异步调用layoutIfNeeded刷新布局,不立即刷新,但layoutSubviews一定会被调用

-layoutIfNeeded方法:如果,有需要刷新的标记,立即调用layoutSubviews进行布局(如果没有标记,不会调用layoutSubviews)

-setNeedsUpdateConstraints方法: 标记为需要更新约束,异步调用updateViewConstraints刷新布局,不立即刷新,但updateViewConstraints一定会被调用

-updateConstraintsIfNeeded方法:如果,有需要刷新的约束,立即调用updateViewConstraints进行布局(如果没有标记,不一定会调用layoutSubviews)

如想要在父视图的layoutSubviews中更新子视图的布局,但是父视图的layoutSubviews没有回调,只回调了子视图的layoutSubviews
可在子视图的layoutSubviews中强制调用父视图的layoutIfNeededsetNeedsLayout

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.superview layoutIfNeeded];
}

3.AutoLayout动画

例:

UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor redColor];
view.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:view];
    
NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:10];
NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:100];
NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:-10];
[self.view addConstraints:@[left, top, right]];
NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:300];
[view addConstraint:height];
    
// 强制布局完成,因为view添加到父视图上后还没布局完成接着就调用了动画,所以在这里强制更新布局,以保证动画的起点正确。
[view.superview layoutIfNeeded];
    
[UIView animateWithDuration:2 animations:^{
    [self.view removeConstraints:@[left, top, right]];
    [view removeConstraint:height];
        
    NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:100];
    NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:100];
    NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:-10];
    [self.view addConstraints:@[left, top, right]];
    NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:300];
    [view addConstraint:height];
    // 开启动画
    [view.superview layoutIfNeeded];
}];
AutoLayout动画.gif

其他文章
iOS-屏幕适配(AutoLayout)

上一篇下一篇

猜你喜欢

热点阅读