Crashalot Crashalot - 2 months ago 42
Swift Question

viewDidLayoutSubviews no longer called when popping top view with UINavigationController

iOS 10, the gift that keeps on breaking, seems to have changed another behavior.

Assume two UIViewControllers are pushed onto a UINavigationController.

On iOS 8/9, calling

navigationController?.popViewController(animated: true)
to pop the top UIViewController (say VC2) caused
viewDidLayoutSubviews
on the bottom view controller (say VC1) to get called.

We relied on this to refresh VC1. Sometimes VC2 adds subviews to VC1 (via the data model), and this needs to get reflected when popping back to VC1.

Accurate frame information is required. We can't use
viewWillAppear
because frame data is wrong on iOS 9. The problem with
viewDidAppear
is that there is a momentary glitch between seeing the view initially and the adjustment.

Now VC1's
viewDidLayoutSubviews
does not get invoked when popping VC2.

1) Is this a bug for
viewDidLayoutSubviews
to not get invoked?

2) What's the right way to refresh view controllers when popping with UINavigationController?

Answer

Relying on viewDidLayoutSubviews was never the proper solution. UIViewController provides viewWillAppear: or viewDidAppear: for such a use. When VC2 is popped from the navigation controller, those two methods will be called on VC1 to let you know that is will be or now is visible again.

viewDidLayoutSubview should only be used to adjust view frames and layout.

viewWill|DidAppear: should be used to handle the view controller becoming visible originally or again. In your case you should use this to update data and add/update views as needed. Those new views should be setup based on the view controller's current frame. They will be adjusted in your implementation of viewDidLayoutSubviews as needed.