究竟什么是 viewDidLayoutSubviews? [英] What's exactly viewDidLayoutSubviews?

查看:21
本文介绍了究竟什么是 viewDidLayoutSubviews?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读UIViewControllerviewDidLayoutSubviews的描述:

被调用来通知视图控制器它的视图刚刚布置了它的子视图[...] 然而,这个方法被调用并不表示视图的子视图的各个布局已经被调整.每个子视图负责调整自己的布局 [...].

Called to notify the view controller that its view has just laid out its subviews [...] However, this method being called does not indicate that the individual layouts of the view's subviews have been adjusted. Each subview is responsible for adjusting its own layout [...].

对我来说,这意味着:当子视图的布局完成时调用,但实际上这不是真的".那么 viewDidLayoutSubviews 的真正背后是什么?

For me, it means: "Called when the layout of subviews is complete, but actually this is not true". So what's really behind viewDidLayoutSubviews?

推荐答案

当ViewControllers View的bounds发生变化时,在子view的位置和大小发生变化后调用该方法.

When bounds change for a ViewControllers View, this method is called after the positions and sizes of the subviews have changed.

  1. 所以这是我们在视图布局好其子视图之后,但在它在屏幕上可见之前对其进行更改的机会.

  1. So this is our chance to make changes to view after it has laid out its subviews, but before it is visible on screen.

必须根据边界进行任何更改,我们可以在此处进行,而不是在 ViewDidLoad 或 ViewWillAppear 中进行.

Any changes that depending on bounds has to be done, we can do here and not in ViewDidLoad or ViewWillAppear.

当 ViewDidLoad &ViewWillAppear,视图的框架和边界是未定稿.因此,当 AutoLayout 完成后,它的工作就是修复 mainView 和是子视图,调用这个方法.

While ViewDidLoad & ViewWillAppear, the frame and bounds of a view are not finalised. So when AutoLayout has done it's job of fixing mainView and it's subviews, this method is called.

使用自动布局时,框架不会每次都调用 layoutSubviews.在这些情况下会调用它.

When using autolayout, framework does not call layoutSubviews every time. This is called in these cases.

  • 旋转设备:只在父视图(响应的viewControllers主视图)上调用layoutSubview

  • Rotating a device: only calls layoutSubview on the parent view (the responding viewControllers primary view)

它自己的边界(不是框架)改变了.(仅当新值不同时,边界才被视为更改,包括不同的原点.)

Its own bounds (not frame) changed. (The bounds are considered changed only if the new value is different, including a different origin.)

注意:viewDidLayoutSubviews 的调用还取决于各种因素,例如自动调整掩码、是否使用自动布局以及视图是否在视图层次结构中.

Note: The call for viewDidLayoutSubviews also depends on various factors like autoresize mask, using Auto-Layout or not, and whether view is in view hierarchy or not.

有关任何其他说明,请查看 layoutSubviews 何时调用?

For any other clarification, check When is layoutSubviews called?

这篇关于究竟什么是 viewDidLayoutSubviews?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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