简体   繁体   中英

Unable to hide status bar on iOS 6,7 when presenting viewcontroller

The following is my custom VC presentation code:

-(void)presentViewController:(UIViewController*)vc
{
    UIWindow *w = [[[UIApplication sharedApplication] delegate] window];
    UIViewController *parentController = (TabBarViewController *)[w rootViewController];

    [parentController addChildViewController:vc];
    if ([vc respondsToSelector:@selector(beginAppearanceTransition:animated:)]) // iOS 6
    {
        [vc beginAppearanceTransition:YES animated:YES];
    }
    UIView *toView = vc.view;
    [parentController.view addSubview:toView];
    toView.frame = parentController.view.bounds;

    CGAffineTransform tr = CGAffineTransformScale(self.view.transform, 1.0f, 1.0f);
    toView.transform = CGAffineTransformScale(self.view.transform, 0.01f, 0.01f);;
    CGPoint oldCenter = toView.center;

    toView.center = ((RootViewControllerEx*)vc).cellCenter;

    [UIView animateWithDuration:4.5 animations:^{
            toView.transform = tr;
            toView.center = oldCenter;
    } completion:^(BOOL finished) {

        [vc didMoveToParentViewController:parentController];
        if ([vc respondsToSelector:@selector(endAppearanceTransition)]) // iOS 6
        {
            [vc endAppearanceTransition];
        }

    }];
}

It works fine, however, in presented VC I am hiding status bar:

- (BOOL)prefersStatusBarHidden {
    return YES;
}

When I present my VC using built-in presentViewController:animated:completion: , status bar in presented VC is hidden. But with my code on iOS 7 status bar is not hidden at all, on iOS 6 it is even more strange - status bar is hidden, but my view size is shorter from top by the size of status bar. So I can see a black gap from top on iOS 6. What should I do to properly hide status bar when using custom VC presentation?

you should try this in your viewDidLoad for differencing the IOS 6/7 status bar problem

 if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)])
{
    //IOS 7 - Status Bar Hidden
    [self prefersStatusBarHidden];
    [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    self.statusBarHidden = YES;
}
else
{
    // iOS 6 - Status Bar shown
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
    self.statusBarHidden = NO;
}

and an method for hiding status Bar

 - (BOOL)prefersStatusBarHidden{
return YES;}

and also add an property for status Bar

@property BOOL statusBarHidden;

then make sure that your view bounds to the screen size and fits correctly

I think this solves your problem :)

Try this

in view did load

   [UIApplication sharedApplication].statusBarHidden = YES;

and set value in plist like

在此处输入图片说明

set this in project summary

在此处输入图片说明

and this in your interface builder

在此处输入图片说明

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