Navigation Bar Changes Height

2019-06-02 06:55发布

问题:

When I push my UIViewController to the screen from my previous controller it animates the change. But when it finishes loading it resizes my navigation bar and the jumpy transition makes it look bad. How can I fix this? All I'm doing is hiding the navigation bar in Controller A in viewWillAppear and showing it in Controller B in viewDidLoad.

回答1:

Ok solved it. In viewDidLoad of Controller B (the view controller I'm pushing) add the following:

UINavigationBar *navigationBar = self.navigationController.navigationBar;

[navigationBar setBackgroundImage:[UIImage new]
                   forBarPosition:UIBarPositionAny
                       barMetrics:UIBarMetricsDefault];

[navigationBar setShadowImage:[UIImage new]];

Then in your UIViewController's XIB make a height constraint on the navigation bar and set it to 68 (from testing the actual line seems to fall in between 68 and 69). Smooth as silk.

edit: If anyone has any better ideas please add them. I'll have to modify this solution for screen rotation so its not perfect.



回答2:

You can do all in your controller A like this:

- (void)viewWillAppear:(BOOL)animated 
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated 
{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [super viewWillDisappear:animated];
}