【问题标题】:iOS Hiding Status bar and tab bar in iOS 6 + 7iOS 6 + 7 中的 iOS 隐藏状态栏和标签栏
【发布时间】:2013-09-24 15:02:25
【问题描述】:

我有一个选项卡式应用程序,在一个选项卡中有一个 UIWebView。当我将设备旋转到横向时,我想让这个 UIWebView 在状态栏和标签栏上全屏显示。

//编辑

好的,现在我已经让它在 iOS 6 中工作了 - 最初在旋转和隐藏标签栏时,它会在标签栏所在的位置留下一个黑色空间,所以 fHeight 代码修复了这个问题。但是在 iOS 6 上它运行良好,但现在它实际上产生了 iOS 6 的黑条问题!!有什么解决方法的想法吗?

- (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];
}

//编辑2

我尝试过使用它,但我不确定我需要传入什么视图?它应该适用于 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);
    }
}

【问题讨论】:

  • 为了您自己的幸福,不要再使用[UIView beginAnimations...][UIView commitAnimations],而是使用基于块的动画方法。

标签: iphone ios objective-c ipad uiwebview


【解决方案1】:

是的,使用适当的UIViewController 轮换方法。隐藏标签栏控制器很容易,但在 iOS 7 上状态栏更难。研究如何做到这一点,你应该没问题。

【讨论】:

  • 在您的更新中,您正在响应设备方向更改通知。没有必要这样做。你有一个视图控制器,它为你提供了确切的方法。我建议在 willAnimateRotationToInterfaceOrientation:duration: 中运行您的代码
  • 好的,我试试看。我在做正确的事情来隐藏标签栏吗?它不起作用..当然它不会在 iOS 7 中隐藏状态栏 - 仅适用于 iOS 6 及更高版本
  • 不,你不是——你永远不会将标签栏控制器推到导航控制器上。不幸的是,隐藏标签栏并不容易。也许您可以做的是在每次方向更改时将一个新的视图控制器推送到您的导航控制器。如果它是横向的,那么它是一个导航控制器,其属性hidesBottomBarWhenPushed 设置为YES,而在纵向上,该值设置为NO
  • 或者只是尝试在方向更改回调的一个视图控制器中使用hidesBottomBarWhenPushed 属性。
  • 它似乎隐藏了 iOS 7 中的状态栏我需要调用 [self setNeedsStatusBarAppearanceUpdate]; 但这会导致 iOS 6 崩溃
【解决方案2】:

试试……

-(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];
}

【讨论】:

  • 那只会一直设置它。而不是在横向时。请看我的编辑
【解决方案3】:

我前段时间搞定了这个工作,忘记发布我的答案,因为我有两个类似的问题!适用于 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];
        }
    }
}

在我的情况下,这是针对我的 webview 的,但理论上你可以给它任何视图。适用于 iOS 6 和 7

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-05
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 2013-11-18
    相关资源
    最近更新 更多