0

How to manipuate with status bar and navigation in canonical way?

So, my goal is to hide and appear status bar and navigation bar in the same time.

What I should do in canonical way, avoiding space between them, avoiding underlaying (status bar lay on navigation bar) and support normal rotation?

I've played with it and now I have code:

-(void)tryToManipulateWithTopBarsIsHidden:(BOOL)hidden{
    if (self.wantsFullScreenLayout) {

        // Get status bar height if visible

        CGFloat statusBarHeight = 0;
        [self topBarsHiddenStateAppearingOrDisappearingStatusBarHidden:hidden
                                                   navigationBarHidden:hidden];
        // Get status bar height if visible
        if (![UIApplication sharedApplication].statusBarHidden) {
            CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
            statusBarHeight = MIN(statusBarFrame.size.height, statusBarFrame.size.width);
//            statusBarOrigin = MIN(statusBarFrame.origin.x,statusBarFrame.origin.y);
        }

        // Set navigation bar frame
        // if status bar is visible and
        // nav bar origin in y is not the same as status bar (magic, really)
        // we need to set nav bar origin y to status bar origin y (auto shifting to bottom)
        CGRect navBarFrame = self.navigationController.navigationBar.frame;
        // if status bar is visible
        if (![UIApplication sharedApplication].statusBarHidden){
            navBarFrame.origin.y = statusBarHeight;
            navBarFrame = CGRectOffset(navBarFrame, 0.0, -20.0);
            self.navigationController.navigationBar.frame = navBarFrame;
        }
        self.navigationController.navigationBar.hidden =[UIApplication sharedApplication].statusBarHidden ;
        LogRect(@"this is statusBar ", [UIApplication sharedApplication].statusBarFrame);
        LogRect(@"this is navigationBar ", navBarFrame);
    }
}

- (void)setTopBarStatusBarHidden:(BOOL)hidden onCompletion:(void(^)(UINavigationBar*))completion andNavigationBar:(UINavigationBar*)bar{
    [[UIApplication sharedApplication] setStatusBarHidden:hidden];
    completion(bar);
}

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

- (void)topBarsHiddenStateAppearingOrDisappearingStatusBarHidden:(BOOL)statusBarHidden
                                             navigationBarHidden:(BOOL)navigationBarHidden{

    UINavigationBar* bar = self.navigationController.navigationBar;
    id fles = self;
    [self setTopBarStatusBarHidden:statusBarHidden
                      onCompletion:^(UINavigationBar *bar) {
                          if (statusBarHidden){
                              [fles setTopBarNavBarHidden:navigationBarHidden];
                          }
                          else
                              [fles setTopBarNavBarHidden:navigationBarHidden];

                      }
                  andNavigationBar:bar];
}

But nothing!

Can anybody explain how to solve this task without this messy (really not working code)?

gaussblurinc
  • 3,642
  • 9
  • 35
  • 64

1 Answers1

0

You just write in the method:

self.navigationController.navigationBar.hidden = YES;
[UIApplication sharedApplication].statusBarHidden = YES;

This will hide the navigation bar and status bar at the same time.

hopper
  • 13,060
  • 7
  • 49
  • 53
Anton
  • 139
  • 5
  • please, read my question: I want to hide/show them together without space and underlaying – gaussblurinc Aug 05 '13 at 13:43
  • Take a look here: https://stackoverflow.com/questions/57517803/how-to-remove-the-default-navigation-bar-space-in-swiftui-navigiationview It might help you. It did nothing for me. I tried every solution listed. Some people have found success. This is for SwiftUI. – user1204493 Aug 05 '20 at 19:35