Jason Pepas
Jason Pepas

Reputation: 444

viewWillAppear not called on contained VC if parent is between viewWillAppear and viewDidAppear

I recently ran into a headache with iOS view controller containment.

Everything works as expected, except in one particular case:

This edge case can come up e.g. if you create and contain a child VC as the result of an asynchronous network call, which might land in-between the parent's viewWillAppear and viewDidAppear.

I've put together a demo here: https://gist.github.com/cellularmitosis/8205610a80112eebd96c

To reproduce this locally, create a new "Single View Application" iOS project in Xcode, then replace the contents of ViewController.swift with the above gist.

Am I missing something obvious here, or is this a bug on Apple's part?

I'm guessing this means I need to override shouldAutomaticallyForwardAppearanceMethods() to return false, and then manually call beginAppearanceTransition and endAppearanceTransition?

Upvotes: 4

Views: 1245

Answers (1)

Jason Pepas
Jason Pepas

Reputation: 444

Follow-up: This is the workaround which I'm currently using: https://gist.github.com/cellularmitosis/56d734ab087a3f283455

I implemented a view controller transition state tracker, and if the parent VC is in-between viewWillAppear and viewDidAppear, then we answer false to shouldAutomaticallyForwardAppearanceMethods.

containChildViewController is updated to handle both true and false for shouldAutomaticallyForwardAppearanceMethods.

Definitely a kludge, but it appears to work.

TODO: There may be an analogous bug during the viewWillDisappear -> viewDidDisappear transition. I haven't checked.

EDIT: edited the workaround gist to use beginAppearanceTransition/endAppearanceTransition instead of directly calling viewWillAppear/viewDidAppear

Upvotes: 0

Related Questions