Should I initialize my viewController to viewDidLoad or viewWillLayoutSubviews

I noticed that sometimes on viewDidLoad I got the size of the view on the right. Sometimes I don’t do it.

for instance

- (void)viewDidLoad { [super viewDidLoad]; self.pullToRefreshController = [self.pullToRefreshController initWithDelegate:self]; PO(self.view); PO(self.view.superview); PO(self.view.superview.superview); PO(self.view.superview.superview.superview); while(false); } -(void)viewWillLayoutSubviews { PO(self.view); PO(self.view.superview); PO(self.view.superview.superview); PO(self.view.superview.superview.superview); while (false); } 

at viewDidLoad the self.view size is still between 320 and 480. In the viewWillLayoutSubviews that have been fixed.

I wonder what happens between them and where should I initialize things? Or what things should be in viewDidLoad and what things should be in viewWillLayoutSubviews?

+4
source share
1 answer

viewDidLoad is a good place to create and initialize the sub items that you want to add to your main view. It is also a good place to further customize your main look. This is also a good place to initialize data structures, because by the time it was called, some properties had to be set on the view controller. Usually this needs to be done only once.

viewWillLayoutSubviews is the place where you place and place subitems, if necessary. This will be called after turns or other events, which will cause the view controller to be set. This can happen many times during the life of a view controller. You should only post submissions here.

+9
source

Source: https://habr.com/ru/post/1447125/


All Articles