Why viewdidlayoutsubviews call multiple times?

孤人 提交于 2019-12-29 03:36:19

问题


I'm developing iOS Universal App on Swift, using auto layout and support only portrait.

I found UIViewController#viewdidlayoutsubviews called multiple times. Instead, viewDidLoad ONLY call once on starting up MyApp's UIViewController.

Why viewdidlayoutsubviews call multiple times? Constraints on each UIView(UIButtons,UITextFields, etc..) will perform in order?

Any Information will be appreciated.


回答1:


LoadView is only called one: when the view needs to be loaded.

LayoutSubviews, however, is called once per run loop on any view that has had setNeedsLayout or setNeedsDisplayInRect called on it - this includes whenever a subview has been added to the view, scrolling, resizing, etc.




回答2:


The best thing to do and this has served me well - is to get in to the habit of adding a log or print statement to methods of which you are unsure

I use this ->

print("Hello From \(NSStringFromSelector(#function))")

or this if inside a View Subclass

Swift.print("Hello From \(NSStringFromSelector(#function))")

It will print the method when it is being called.



来源:https://stackoverflow.com/questions/27014360/why-viewdidlayoutsubviews-call-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!