简体   繁体   中英

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

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!

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.

如果您的表每次需要使用新数据加载数据,则该表应位于viewWillAppear下;否则,如果该表需要由一个不变的单个Data重新加载,或者对Data不执行任何编辑操作,则应使用viewDidload

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM