Changing the NavigationBar's height in 'viewDidAppear' and 'viewDidLoad', Why is it different?

| | August 7, 2015

I just want to reduce the height of the NavigaitonBar.

I put the following code in ViewDidLoad then I ran the project. The height changed in a moment, but it was restored to the default height shortly .

CGRect navRect  = self.navigationController.navigationBar.frame;
navRect.size.height = 30;
self.navigationController.navigationBar.frame = navRect;

And I tried to put these code in viewWillAppear, the same issue happened again!

But in viewDidAppear, it works properly, the height was changed.

I always add some barButtonItem in viewDidLoad. It illustrates that the NavigationController has been successfully initialized in viewDidLoad,but why can’t the height be changed?

This confused me for a long time.

Thanks!

2 Responses to “Changing the NavigationBar's height in 'viewDidAppear' and 'viewDidLoad', Why is it different?”

  1. if your table needs to load data every time with new Data then it should be under viewWillAppear
    Otherwise, if the table needs to be reload by a single Data which doesn’t vary or there is not any editing operation performed on Data , you should use viewDidload

  2. Since Auto Layout was added the lifecycle of views also was changed.
    All geometric modifications of subviews in view should be done in viewDidLayoutSubviews method.
    As you can see from the name of this method it means that all subviews will have placed properly at the moment when this method will be called.

Leave a Reply