2

我有一个选项卡式应用程序,在一个选项卡中有一个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];
}

//编辑

我试过使用它,但我不确定如何正确传递视图 - 我试过self.viewwebView 和其他,但我无法让它在 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);
    }
}
4

4 回答 4

6

在 Github 上创建了一个公开的 Gist来说明我们如何做到这一点。

由于@Chris Byatt和我们的团队进行了尝试,这个解决方案已经经历了几次迭代。因此,请确保从那里下载最新版本。

方法签名已简化为

- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated;

你可以在你的UIViewController子类中这样调用它:

[self.tabBarController setTabBarHidden:YES animated:YES];
于 2013-09-25T13:46:53.350 回答
3

好的,伙计们,在为此苦苦挣扎了大约两个小时后,我的同事教了我正确的方法。实际上有一个非常简单的修复方法,我在网上找不到:

只是说:

        self.hidesBottomBarWhenPushed = YES;

在你的 viewController 的 init 方法中,注释掉 self.tabBar.hidden 相关代码。

于 2013-12-06T10:20:33.947 回答
2
-(void) hideBottomTabs{
// Get the size of the main screen
CGRect fullScreenRect = [[UIScreen mainScreen]bounds];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0){
    UITabBar *bar = ((UITabBarController *)self.parentViewController).tabBarController.tabBar;
    fullScreenRect.size.height += ViewHeight(bar);
}
// Hide the tab bar
((UITabBarController *)self.parentViewController).tabBarController.tabBar.hidden = YES;
// Resize and fill the screen
[[((UITabBarController *)self.parentViewController).view.subviews objectAtIndex:0] setFrame:fullScreenRect];

}

我已经以这种方式解决了我的问题。

于 2013-10-16T04:34:27.473 回答
2

我最终得到了这个工作,但花了一段时间。它源于我的原始代码和一些 JRG-Developers 工作的混合。

- (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

于 2013-09-27T10:10:18.537 回答