什么时候应该重写viewDidLayoutSubviews?

原文:When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method. 

关键:the bounds change, 任何依赖于bounds/frame的约束都应该在viewDidLayoutSubviews之后设置,而不应该放在viewDidLoadviewWillAppear这些中,也最好不要放在viewDidAppear中。因为viewDidLayoutSubviewsviewWillAppear之后和viewDidAppear之前执行,因此在viewWillAppear时,使用auto layout布局view的bounds/frame还未确定,如果此时获取bounds/frame,肯定是不正确的,而放在viewDidAppear之中来设置依赖于其他view的bounds/frame的view又有些晚。

注意: 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只能代表当前controller下的view的layout被调整完成,并不代表当前view下所有subviews的layout调整完成。

你可能感兴趣的:(IOS,Swift)