使用 UINavigationController 弹出顶视图时不再调用 viewDidLayoutSubviews [英] viewDidLayoutSubviews no longer called when popping top view with UINavigationController

查看:20
本文介绍了使用 UINavigationController 弹出顶视图时不再调用 viewDidLayoutSubviews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS 10,不断破碎的礼物,似乎已经改变了另一种行为.

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

假设两个 UIViewController 被推送到一个 UINavigationController 上.

Assume two UIViewControllers are pushed onto a UINavigationController.

在 iOS 8/9 上,调用 navigationController?.popViewController(animated: true) 来弹出顶部 UIViewController(比如 VC2)导致底部视图控制器上的 viewDidLayoutSubviews(说 VC1) 被调用.

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.

我们依靠这个来刷新 VC1.有时 VC2 会向 VC1 添加子视图(通过数据模型),这需要在弹回 VC1 时得到反映.

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.

需要准确的帧信息.我们不能使用 viewWillAppear 因为 iOS 9 上的帧数据错误.viewDidAppear 的问题是在最初看到视图和调整之间存在短暂的故障.

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.

现在 VC1 的 viewDidLayoutSubviews 在弹出 VC2 时不会被调用.

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

1) 这是 viewDidLayoutSubviews 没有被调用的错误吗?

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

2) 使用 UINavigationController 弹出时刷新视图控制器的正确方法是什么?

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

推荐答案

依赖 viewDidLayoutSubviews 从来都不是正确的解决方案.UIViewController 提供了 viewWillAppear:viewDidAppear: 用于此类用途.当 VC2 从导航控制器中弹出时,将在 VC1 上调用这两个方法,让您知道将再次可见或现在再次可见.

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 应该只用于调整视图框架和布局.

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

viewWill|DidAppear: 应该用于处理视图控制器最初或再次变为可见.在您的情况下,您应该使用它来更新数据并根据需要添加/更新视图.这些新视图应该基于视图控制器的当前帧进行设置.它们将根据需要在您的 viewDidLayoutSubviews 实现中进行调整.

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.

这篇关于使用 UINavigationController 弹出顶视图时不再调用 viewDidLayoutSubviews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆