简体   繁体   中英

iOS: Status bar and Navigation bar: Space or Underlaying

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)?

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.

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