viewDidLayoutSubviews no longer called when popping top view with UINavigationController

北战南征 提交于 2019-12-12 09:57:08

问题


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?


回答1:


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.

viewDidLayoutSubviews 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.




回答2:


I will complement rmaddy's answer. You need to decouple performing layout and updating your data. If your flow is such that data should updated as the view is about to appear, you should update your controller's view-backing data in viewWillAppear:, reload your views and then mark the view as needing layout using setNeedsLayout. This will cause the system to perform a layout on the controller's view, and will trigger the layout. This way, you can ensure the layout is performed once the view is ready, and not before (as often the case is in viewWillAppear:.



来源:https://stackoverflow.com/questions/39780470/viewdidlayoutsubviews-no-longer-called-when-popping-top-view-with-uinavigationco

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!