0

I have a UiTabbar controller with 4 views. I Also have a button that I added (to the tab bar). That presents a modal view controller. in the viewDidAppear method of my modal view controller I have the following code

[self setWantsFullScreenLayout:YES];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:YES];

For some reason, the first time it loads, the view is shifted down 20 pix. If I dismiss the view controller, and then show it again, the view is in the desired position. Where should I place this code so that the view is always in the correct spot.

user379468
  • 3,989
  • 10
  • 50
  • 68

1 Answers1

0

First we check for whether if statusBar is already hidden or not via [[UIApplication sharedApplication] isStatusBarHidden], if it is not hidden (== being shown), we hide it! Else, do otherwise!

  • To fix 20px when status is shown - but navigation is not properly pushed down, just add 20 point to origin.y of self.navgigationController.navigationBar.frame.

  • Do the same when we want to hide status bar, just remove that 20 point to origin.y of self.navgigationController.navigationBar.frame so just leave it 0.

I also encountered same problem with you, and here is my sample code, you can configure it as you like:

@implementation SomeViewController {
    BOOL isStatusBarEnabled;
}

// ...

- (void)toggleStatusBar
{
    UINavigationBar *navBar = self.navigationController.navigationBar;

    if ([[UIApplication sharedApplication] isStatusBarHidden]) {

        // Change to regular mode
        // Show status bar
        [[UIApplication sharedApplication] setStatusBarHidden:NO
                                                withAnimation:UIStatusBarAnimationSlide];
        [UIView animateWithDuration:0.3
                         animations:^{
                             navBar.frame = CGRectMake(navBar.frame.origin.x, 20, navBar.frame.size.width, navBar.frame.size.height);
                         } completion:nil];

    } else if (![[UIApplication sharedApplication] isStatusBarHidden]) {
        // Change to fullscreen mode
        // Hide status bar
        [[UIApplication sharedApplication] setStatusBarHidden:YES
                                                withAnimation:UIStatusBarAnimationSlide];
        [UIView animateWithDuration:0.4
                         animations:^{
                             navBar.frame = CGRectMake(navBar.frame.origin.x, 0, navBar.frame.size.width, navBar.frame.size.height);
                         } completion:nil];
    }

}

// ...

... then, in my case, I have a setting key to let user choose toggle show/hide status bar.

// ...

- (void)onDefaultsChanged:(NSNotification*)aNotification
{

    NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
    isStatusBarEnabled = [standardDefaults boolForKey:kStatusBar];

    if (isStatusBarEnabled) {

      if ([[UIApplication sharedApplication] isStatusBarHidden]) {

          // Change to regular mode
          // Show status bar
          [self toggleStatusBar];   

    } else {

        // Change to fullscreen mode
        // Hide status bar
        [self toggleStatusBar];

  }

  // ...
}

that's it!

Vinh Nguyen
  • 816
  • 1
  • 13
  • 27