简体   繁体   中英

How to stop the status bar covering the navigation bar in iOS6

This is my issue....

在此处输入图片说明

This happens when:

  1. Status bar is faded out using the button.
  2. Rotate 180 deg from landscape to upside-down landscape.
  3. Status bar is faded back in using the button. - it now covers the navigation bar.

The button code to toggle the status bar visibility:

- (IBAction)toggleBar:(id)sender
{
    NSLog(@"View Frame : %@", NSStringFromCGRect(self.view.frame));

    // Toggle status bar visiblity
    BOOL isStatusBarHidden = [[UIApplication sharedApplication] isStatusBarHidden];
    [[UIApplication sharedApplication] setStatusBarHidden:!isStatusBarHidden
                                            withAnimation:UIStatusBarAnimationFade];
}

The view always reports its frame to be 480 x 288.

The issue was fixable on iOS 5 using a hacky workaround, by stopping the rotation filling the space.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIApplication sharedApplication] isStatusBarHidden])
    {
        float oldAlpha = self.navigationController.navigationBar.alpha;
        self.navigationController.navigationBar.alpha = 0.01;
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

        double delayInSeconds = 0.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            self.navigationController.navigationBar.alpha = oldAlpha;
            [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
        });
    }

    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

This dons't work on iOS 6 because shouldAutorotateToInterfaceOrientation is not called. However, using its replacement: willRotateToInterfaceOrientation also dosn't work. Any ideas?

切换状态栏后,再次设置您的self.view.frame。

At IOS6, you should use these methods. Check these out.

- (BOOL)shouldAutorotate
{
    //returns true if want to allow orientation change
    return TRUE;
}
- (NSUInteger)supportedInterfaceOrientations
{   
     //decide number of origination tob supported by Viewcontroller.
     return UIInterfaceOrientationMaskAll;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    //from here you Should try to Preferred orientation for ViewController 
}

Ok the answer was to use the same hack on willRotateToInterfaceOrientation I had said this would not work in my question, but I just tried it again and it did the trick.

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if ([[UIApplication sharedApplication] isStatusBarHidden])
    {
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

        double delayInSeconds = 0.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
        });
    }
}

After I call

dismissViewControllerAnimated

this method, and then call

[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];

this problem appear.

When I call [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide]; first, no problem.

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