viewDidLayoutSubviews 到底是什么? [英] What's exactly viewDidLayoutSubviews?

查看:34
本文介绍了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 视图的边界发生变化时,会在子视图的位置和大小发生变化后调用此方法.

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天全站免登陆