Is there an iOS method that fires when Autolayout has completed?

删除回忆录丶 提交于 2019-11-30 17:41:45

I'm using viewDidLayoutSubviews for this. Apple's documentation says, "Called to notify the view controller that its view has just laid out its subviews."

The following can be used to avoid multiple calls:

- (void) didFinishAutoLayout {

    // Do some stuff here.    

    NSLog(@"didFinishAutoLayout");
}

and

- (void) viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];


    [NSObject cancelPreviousPerformRequestsWithTarget:self
                                 selector:@selector(didFinishAutoLayout)
                                           object:nil];
    [self performSelector:@selector(didFinishAutoLayout) withObject:nil
               afterDelay:0];
}

What it worked in my case was request layout after changed a constraint value:

    self.cnsTableviewHeight.constant = 50;
    [self layoutIfNeeded];

Later on override layoutSubviews method:

    - (void) layoutSubviews { //This method when auto layout engine finishes
    }

You can call setNeedsLayout also instead of layoutIfNeeded

I guess implementing viewDidLayoutSubviews is the correct way but I used an animation just to write the completion callback inside the same method.

someConstraint.constant = 100; // the change

// Animate just to make sure the constraint change is fully applied
[UIView animateWithDuration:0.1f animations:^{
    [self.view setNeedsLayout];
} completion:^(BOOL finished) {
    // Here do whatever you need to do after constraint change
}];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!