Frustrated with view initialization in loadView

I do everything programmatically when developing for ios, so I manually initialize the view controller view in loadView for all my view controllers (this is what Apple says). What upsets me is the viewcontrollers frame, initially this {{0, 20}, {320, 460}} incorrectly takes into account any navbars or tabs that I have in my application. The view finally sets its proper frame in viewDidAppear, but by then it's too late to do anything. In any case, in order to recognize the layout of a view earlier, without calculating it manually?

This is disappointing, because I always have to manually check whether the phone is in landscape or portrait mode to account for rotation, and if I need to configure my routines in a certain way, which I can not do with autoresistance, then I also have to perform manual calculations.

Is there any template that Apple recommends for this, or is it the same if it recognizes the layout of the view before, without manually calculating it?

+4
source share
2 answers

If you are using iOS 5, you need to pass the correct frame in the viewDidLayoutSubviews and viewWillLayoutSubviews your subclass of UIViewController. Here you have to calculate your layout, and they call when the device rotates.

Otherwise, viewWillAppear exists from iOS 2.0, and my test application shows that the correct frame has been passed to it. It does not solve the rotation in the middle of the turn, but presumably you can just connect to it elsewhere. You are not going to customize your views at the end of the game, but you are not playing with layoutIfNeeded from viewDidLoad (I was not able to work in the past), I do not know what you should do pre iOS5.

+4
source

You can make sure that your view has the correct autoresist mask, and it should be adjusted as soon as a new size appears.

 [view setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; 

What I usually do. Otherwise, I will adjust the layout in a later callback - it is set to the viewWillAppear navigation bar, not sure if viewDidLoad is too early.

0
source

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


All Articles