简体   繁体   English

iOS 6 + 7中的iOS隐藏状态栏和标签栏

[英]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. 我有一个选项卡式应用程序,并且在一个选项卡中有一个UIWebView。 When I rotate the device to landscape I'd like to make this UIWebView full screen over the status bar and tab bar. 当我将设备旋转到横向时,我想在状态栏和选项卡栏上全屏显示该UIWebView。

//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. 好的,现在我可以在iOS 6中使用它了-最初在旋转和隐藏选项卡栏时,它会在选项卡栏所在的位置留下一个黑色空间,因此fHeight代码可以解决此问题。 However on iOS 6 it worked perfectly, but now it actually creates the black bar problem iOS 6 was having!! 但是,在iOS 6上,它运行良好,但是现在它实际上造成了iOS 6出现黑条的问题! 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 //编辑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 它应该适用于iOS 6和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. 是的,请使用适当的UIViewController旋转方法。 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. 隐藏选项卡栏控制器很容易,但是在iOS 7上,状态栏更加困难。研究如何做到这一点,您应该会很好。

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 适用于iOS 6和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 适用于iOS 6和7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM