ios 什么时候调用 ViewWillLayoutSubviews 和 ViewDidLayoutSubviews 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23457391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When are the ViewWillLayoutSubviews and ViewDidLayoutSubviews methods called?
提问by Waseem05
When do the ViewWillLayoutSubviews
and ViewDidLayoutSubviews
methods of UIViewController get called?
What is the status of the view's frame before willLayoutSubviews
?
什么时候调用 UIViewController的ViewWillLayoutSubviews
和ViewDidLayoutSubviews
方法?视图框架之前的状态是什么willLayoutSubviews
?
回答by cmyr
from the documentation:
从文档:
When a view's bounds change, the view adjusts the position of its subviews. Your view controller can override this method to make changes before the view lays out its subviews. (Link)
当视图的边界发生变化时,视图会调整其子视图的位置。您的视图控制器可以覆盖此方法以在视图布局其子视图之前进行更改。(链接)
viewWillLayoutSubviews
gets called anytime your view controller's view has its bounds changed. This happens when the view is loaded, when a rotation event occurs, or when a child view controller has its size changed by its parent. (There are probably some other situations, too). If there is anything you need to update before that view lays itself out (and before your constraints are re-applied) you should do it here. you should generally notupdate constraints here, because updating constraints can cause another layout pass.
viewWillLayoutSubviews
每当您的视图控制器的视图更改其边界时都会被调用。当视图被加载、旋转事件发生或子视图控制器的大小被其父视图控制器改变时,就会发生这种情况。(可能还有其他一些情况)。如果在该视图显示之前(以及重新应用约束之前)需要更新任何内容,则应在此处执行。你通常不应该在这里更新约束,因为更新约束会导致另一个布局传递。
viewDidLayoutSubviews
is called once all of your subviews have been laid out. If you need to fine-tune that layout by manually adjusting frames, for instance, this would be the place to do it.
viewDidLayoutSubviews
一旦您的所有子视图都已布置好,就会被调用。例如,如果您需要通过手动调整框架来微调布局,这将是您的最佳选择。
view controller lifecycle can be a bit confusing, but it's worth trying to really understand if you're going to be doing much iOS development. This articleis a really good overview.
视图控制器生命周期可能有点令人困惑,但是如果您要进行大量的 iOS 开发,那么真正了解它是值得的。这篇文章是一个非常好的概述。