Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is viewDidLayoutSubviews called multiple times?

Tags:

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

I found UIViewController.viewDidLayoutSubviews called multiple times. Instead, viewDidLoad is only called once on starting up MyApp's UIViewController.

Why is viewDidLayoutSubviews called multiple times? Will constraints on each UIView (UIButtons,UITextFields, etc..) be performed in order?

Any information will be appreciated.

like image 389
Michitaka Avatar asked Nov 19 '14 10:11

Michitaka


People also ask

How many times viewDidLayoutSubviews called?

Ans : ViewDidLoad called once only when all views are loaded. viewDidLayoutSubviews : Apple gave a very good explanation on this by saying that it is called to notify the view controller that its view has just laid out its subviews.

How many times is viewDidLoad called?

viewDidLoad() is one of the initialization methods that is called on the initial view controller. viewDidLoad() is called before anything is shown to the user - and it is called only once.

What is viewDidLayoutSubviews in Swift?

Called to notify the view controller that its view has just laid out its subviews.

What is layoutIfNeeded in Swift?

layoutIfNeeded()Lays out the subviews immediately, if layout updates are pending.


1 Answers

LoadView is only called once: 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.

like image 65
Hayley Guillou Avatar answered Sep 22 '22 21:09

Hayley Guillou