For example, there is an empty single-view project. Basically view, one view is created and using IB constraints is bound to borders at a distance of 20.

In ViewController, in the viewDidLoad method, I output to the console:

NSLog(@"%f %f",[[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height); NSLog(@"%f %f", testView.frame.size.width, testView.frame.size.height); 

The result after launching in the iPhone4 simulator:

 320.000000 480.000000 - разрешение экрана 280.000000 528.000000 - размер testView 

The size is not correct, although everything is displayed correctly. The correct size is 280x440.

Which method works with the dimensions of the testView so that they are correct?

    1 answer 1

    Rendering methods of the view:

      - (void)setNeedsLayout;//Сетим флажок что нужно пересчитать лайауты при следующем цикле ранлупа при перерисовке экрана. - (void)layoutSubviews;//Метод вызываетя при перерисовки экрана, здесь обычно задавались раньше фреймы для новых позиций вьюх, сейчас после вызова супера можно брать новые фреймы так как вызывается после -(void)updateConstraints; - (BOOL)needsUpdateConstraints;//Проверка флага на перещет констрайнов. - (void)layoutIfNeeded; //Перериосвать лайауты если поставлен флажок методом - (void)setNeedsLayout; - (void)setNeedsUpdateConstraints; // Вызывается если нужно зафиксировать изменение констрайнов, для следующего пересчета в ранлупе. - (void)updateConstraints;//Метод вызывается для перемчета констрайнов. Обычно тут его перегружают для задания новых констрейнов и анимации. 

    For UIViewController there are methods:

     - (void)viewWillLayoutSubviews; // Ща будут пересчитаны лайауты у вьюхи контроллера. - (void)viewDidLayoutSubviews;// Пересчитаны лайауты у вьюхи контроллера. 

    For reference, the ViewDidLoad method is called when the view is loaded, and you still need to place it in the view hierarchy and on the screen, only then you can find out the size of the frames. Read the documentation about controller methods:

     - (void)viewDidAppear:(BOOL)animated - (void)viewWillAppear:(BOOL)animated - (void)viewDidDisappear:(BOOL)animated - (void)viewWillDisappear:(BOOL)animated - (void)viewWillLayoutSubviews - (void)viewDidLayoutSubviews 

    I write from memory better in the documentation read about these methods.

    • Thank. Blunted and did not look at the methods associated with layouts. Moving on to the next stage of dealing with autoLayout. - Zatoichy