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

Kent picture Kent · May 9, 2014 · Viewed 10.1k times · Source

I have an iOS app in which I need to know when a new view is completely visible on-screen; that is, when Autolayout has finished its calculations and the view has finished drawing.

ViewDidAppear seems to fire well before the view is completely visible. If I turn off Autolayout, the timing seems to line up as far as human perception goes, but I need to use Autolayout in this project (so this isn't a solution...just a test).

Is there any method that fires when Autolayout is done calculating? Or another method that fires when the view is ACTUALLY visible (since ViewDidAppear doesn't work for this)?

Thanks!

Answer

Matt picture Matt · Oct 15, 2014

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];
}