IOS 7 隐藏选项卡栏问题

4

隐藏IOS7中的选项卡栏会显示不正常的行为

当我使用以下代码时:

self.tabBarController.tabBar.hidden = YES;

上面的代码隐藏了选项卡,但是我的底部视图不再具有交互性。
但是当我在导航中推送视图控制器之前使用它时。
someViewController.hidesBottomBarWhenPushed = YES
[self.navigationController pushViewController:someViewController animated:YES];

它可以隐藏选项卡栏,底部视图也是交互式的。 但在这种情况下的问题是,当我们弹出视图控制器时,它会在选项卡栏上方显示黑色条形栏,持续几秒钟。

5个回答

10

我希望你已经得到了解决方案。只是为了确保,你尝试过了吗?

 self.edgesForExtendedLayout = UIRectEdgeBottom;

0

所以我已经将一些用Objective-C编写的答案改写成了Swift 3.0,认为它会起作用。以下是代码:

func hideTabBar() {
    let tabBarControllerView = self.tabBarController?.view
    if let tabBarControllerSubviews = tabBarControllerView?.subviews {
        for view in tabBarControllerSubviews {
            if view is UITabBar {
                view.frame = CGRect(
                    x: view.frame.origin.x,
                    y: (UIScreen.main.bounds.size.height == 568 ? 568 : 480) + 20,
                    width: self.view.frame.size.width,
                    height: self.view.frame.size.height
                )
            } else {
                view.frame = CGRect(
                    x: view.frame.origin.x,
                    y: view.frame.origin.y,
                    width: self.view.frame.size.width,
                    height: UIScreen.main.bounds.size.height == 568 ? 568 : 480
                )
            }
        }
    }
}

func showTabBar() {
    let tabBarControllerView = self.tabBarController?.view
    if let tabBarControllerSubviews = tabBarControllerView?.subviews {
        for view in tabBarControllerSubviews {
            if view is UITabBar {
                view.frame = CGRect(
                    x: view.frame.origin.x,
                    y: (UIScreen.main.bounds.size.height == 568 ? 519 : 431),
                    width: self.view.frame.size.width,
                    height: self.view.frame.size.height
                )
            } else {
                view.frame = CGRect(
                    x: view.frame.origin.x,
                    y: view.frame.origin.y,
                    width: self.view.frame.size.width,
                    height: UIScreen.main.bounds.size.height == 568 ? 519 : 431
                )
            }
        }
    }
}

0

如果您想要隐藏/显示视图的 UITabBarController,可以尝试以下方法:

隐藏 tabbar 的方法如下:

 - (void)hideTabBar:(UITabBarController *) tabbarcontroller
 {
     for(UIView *view in tabbarcontroller.view.subviews)
     {
        if([view isKindOfClass:[UITabBar class]])
       {
           [view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
        }
        else
        {
           [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
        }
     }
  }

显示选项卡栏:

   - (void)showTabBar:(UITabBarController *) tabbarcontroller
     {

         for(UIView *view in tabbarcontroller.view.subviews)
         {
            if([view isKindOfClass:[UITabBar class]])
            {
               [view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
            }
            else
            {
               [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
            }
         }
      }

也许这会有所帮助。


0

我认为你们两个问题都是由于未定义/缺失autoresizingMask或缺少自动布局约束(无论你使用哪种)。

当隐藏选项卡栏时,UITabBarController会拉伸其view以使其tabBar超出屏幕。您包含的视图控制器的视图应适当地拉伸以使用新空间,否则您将获得空白空间和/或非交互区域。

编辑:

刚刚意识到隐藏选项卡栏不在默认SDK中,而是在我很久以前创建的category中。

无论如何,我认为拉伸UITabBarController的视图框架是“隐藏”选项卡栏(实际上将其移出屏幕)最优雅的方法,因为您不必处理子视图或直接查找选项卡栏框架。


-1
使用以下代码来解决您的问题:

隐藏:

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

        float fHeight = screenRect.size.height;
        if(  UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
        {
            fHeight = screenRect.size.width;
        }

        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)];
                view.backgroundColor = [UIColor blackColor];
            }
        }
    }

显示:

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

    if(  UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
    {
        fHeight = screenRect.size.width - 49.0;
    }
    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)];
        }
    }
}

根据您的需求,在viewWillAppear和设备旋转方法中使用这些方法


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接