简体   繁体   中英

iOS Hiding Status bar and tab bar in iOS 6 + 7

I've got a tabbed application and in one tab there is a UIWebView. When I rotate the device to landscape I'd like to make this UIWebView full screen over the status bar and tab bar.

//Edit

Ok, so now I've got it working in iOS 6 - originally when rotating and hiding the tab bar it would leave a black space where the tab bar was, so the fHeight code fixes this. However on iOS 6 it worked perfectly, but now it actually creates the black bar problem iOS 6 was having!! Any ideas for a workaround to this?

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
{
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        [self hideTabBar:self.tabBarController];
        [[UIApplication sharedApplication] setStatusBarHidden:TRUE withAnimation:UIStatusBarAnimationSlide];
    }
    else
    {
        [self showTabBar:self.tabBarController];
        [[UIApplication sharedApplication] setStatusBarHidden:FALSE withAnimation:UIStatusBarAnimationSlide];
    }
}

- (void) hideTabBar:(UITabBarController *) tabbarcontroller
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    float fHeight = screenRect.size.height;
    if(  UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
    {
        fHeight = screenRect.size.width;
    }

    for(UIView *view in self.tabBarController.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
        }
        else
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
            view.backgroundColor = [UIColor blackColor];
        }
    }
    [UIView commitAnimations];
}

- (void) showTabBar:(UITabBarController *) tabbarcontroller
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    float fHeight = screenRect.size.height - 49.0;

    if(  UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
    {
        fHeight = screenRect.size.width - 49.0;
    }

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    for(UIView *view in tabbarcontroller.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
        }
        else
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
        }
    }
    [UIView commitAnimations];
}

//Edit 2

I've tried using this but I'm not sure what view I need to pass in? It's supposed to work for iOS 6 and 7

- (void)setTabBarHidden:(BOOL)hidden view:(UIView *)view animated:(BOOL)animated
{
    if (self.tabBar.hidden == hidden)
        return;

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    float height = 0.0f;

    if(UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
    {
        height = screenRect.size.width;
    }
    else
    {
        height = screenRect.size.height;
    }

    if (!hidden)
    {
        height -= CGRectGetHeight(self.tabBar.frame);
    }

    void (^workerBlock)() = ^() {

        self.tabBar.frame = CGRectMake(CGRectGetMinX(self.tabBar.frame), height, CGRectGetWidth(self.tabBar.frame), CGRectGetHeight(self.tabBar.frame));
        view.frame = CGRectMake(CGRectGetMinX(view.frame), CGRectGetMinY(view.frame), CGRectGetWidth(view.frame), height);
    };

    void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
        self.tabBar.hidden = hidden;
    };

    if (animated)
    {
        [UIView animateWithDuration:0.25f animations:workerBlock completion:completionBlock];
    }
    else
    {
        workerBlock();
        completionBlock(YES);
    }
}

Yes, use the appropriate UIViewController rotation methods. Hiding the tab bar controller is easy enough, but the status bar is more difficult on iOS 7. Research how to do that and you should be fine.

Try...

-(void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [self setHidesBottomBarWhenPushed:YES];
    [super viewWillApper:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [self setHidesBottomBarWhenPushed:NO];
    [super viewWillDisapper:animated];
}

I got this working a while ago and forgot to post my answer as I had two similar questions going! Works in iOS 6 and 7

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {

    BOOL toLandscape = UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);

    CGRect screenRect = [[UIScreen mainScreen] bounds];

    void (^workerBlock)() = ^() {


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

        float height = toLandscape ? screenRect.size.width : screenRect.size.height - CGRectGetHeight(self.tabBarController.tabBar.frame);

        float width = toLandscape ? screenRect.size.height : screenRect.size.width;

        webView.frame = CGRectMake(CGRectGetMinX(webView.frame),
                               CGRectGetMinY(webView.frame),
                               width,
                               height);

        [self moveTabBarToPosition:height];
    };

    [UIView animateWithDuration:0.25f animations:workerBlock];
}
//Moving the tab bar and its subviews offscreen so that top is at position y
-(void)moveTabBarToPosition:(int)y {
    self.tabBarController.tabBar.frame = CGRectMake(self.tabBarController.tabBar.frame.origin.x, y, self.tabBarController.tabBar.frame.size.width, self.tabBarController.tabBar.frame.size.height);

    for(UIView *view in self.tabBarController.view.subviews) {
        if ([view isKindOfClass:[UITabBar class]]) {
            [view setFrame:CGRectMake(view.frame.origin.x, y, view.frame.size.width, view.frame.size.height)];
        } else {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, y)];
            view.backgroundColor = [UIColor blackColor];
        }
    }
}

In my case this is for my webview but theoretically you can give it any view. Works in iOS 6 and 7

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