How do I get the navigation bar in a UINavigationController to update its position when the status bar is hidden?

前端 未结 4 1911
长发绾君心
长发绾君心 2020-12-05 14:35

I have a UINavigationController with a visible Navigation Bar. I have one particular UIViewController which I\'d like to hide the status bar when p

相关标签:
4条回答
  • 2020-12-05 15:19

    I found the solutions above didn't quite work when the view is in the middle of an animated presentation, so I deferred hiding the status bar as follows:

    -(void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        // doesn't work immediately because modal view has to finish loading
        dispatch_async(dispatch_get_main_queue(), ^(){
            [[UIApplication sharedApplication] setStatusBarHidden:YES];
            self.view.frame = [UIScreen mainScreen].applicationFrame;
        });
    }
    
    0 讨论(0)
  • 2020-12-05 15:24

    You should try resizing the frame of your UIViewControllers's view after you have hidden the StatusBar. The applicationFrame updates it's origin.y and size.height during the setStatusBarHidden:animated: method.

    CGRect rect = [UIScreen mainScreen].applicationFrame;
    self.view.frame = rect;
    
    [self.view setNeedsLayout];
    
    0 讨论(0)
  • 2020-12-05 15:24

    In your root view controller (where you want to show the status bar):

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        [[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];;
    }
    

    In the view controller you push on the stack (where you want to hide the status bar):

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];;
    }
    

    Edit:

    I realize now you want to hide the status bar. Got them mixed up since you were showing/hiding the nav bar in the code you posted. My mistake. It's essentially the same code, anyway:

    In your root view controller (where you want to show the status bar):

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        [[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
    }
    

    In the view controller you push on the stack (where you want to hide the status bar):

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
    }
    

    I just tested this with an existing project and it worked.

    0 讨论(0)
  • 2020-12-05 15:36

    There are two ways that you can do what you are asking.

    One is to manually move the navigation bar:

    In viewWillAppear:

        [UIApplication sharedApplication].statusBarHidden = YES;
        self.view.frame = [UIScreen mainScreen].applicationFrame;
        CGRect frame = self.navigationController.navigationBar.frame;
        frame.origin.y = 0;
        self.navigationController.navigationBar.frame = frame;
    

    In viewWillDisappear:

        [UIApplication sharedApplication].statusBarHidden = NO;
        CGRect frame = self.navigationController.navigationBar.frame;
        frame.origin.y = 20.0;
        self.navigationController.navigationBar.frame = frame;
    

    Things will also be okay if you are willing to turn off the navigation bar as well, although I suspect that is not what you wanted:

        [UIApplication sharedApplication].statusBarHidden = YES;
        self.navigationController.navigationBarHidden = YES;
    
    0 讨论(0)
提交回复
热议问题