IOS 7 hide tabbar issue

2019-06-24 15:43发布

Hide tabbar in IOS7 shows informal behaviour

When I use

self.tabBarController.tabBar.hidden = YES;

Above code hides the tabBar, but my view from bottom does not remain interactive

But when I uses this just before pushing viewController in navigation

someViewController.hidesBottomBarWhenPushed = YES
[self.navigationController pushViewController:someViewController animated:YES];

It hides tabbar as well as view from bottom is also interactive. but problem in this case is, when we pop viewController it shows black bar just above tabbar for few seconds.

5条回答
该账号已被封号
2楼-- · 2019-06-24 16:18

Use following code to resolve your problem

Hide :

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

Show :

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

Use this methods in viewWillAppear and on device rotation methods as per your requirement

查看更多
Luminary・发光体
3楼-- · 2019-06-24 16:20

I hope you got the solution. Just to make sure, did you try

 self.edgesForExtendedLayout = UIRectEdgeBottom;
查看更多
混吃等死
4楼-- · 2019-06-24 16:20

So I've rewritten some of the answers written in Objective-C into Swift 3.0 thinking it would work. Here's the code:

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
                )
            }
        }
    }
}
查看更多
▲ chillily
5楼-- · 2019-06-24 16:22

Try this, if you want to hide/show the UITabBarController of view:

For hide the 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)];
        }
     }
  }

for show Tabbar:

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

may be it will help.

查看更多
Deceive 欺骗
6楼-- · 2019-06-24 16:22

I think that both of you problems are due to not well defined/missing autoresizingMask's or missing auto-layout constraints (whichever you're using).

What UITabBarController does when hiding the tab bar is to stretch its view enough to get the its tabBar outside the screen. Your contained view controllers' views in turn should properly stretch to use the new space or you'll get empty spaces and/or non interactive zones.

Edit:

Just realized that hiding the tab bar is not in the default SDK but in a category I made long time ago.

Anyway stretching UITabBarController's view frame seems to me the most elegant way to "hide" the tab bar (actually move it away from the screen) as you don't have to deal with subviews or hunt down the tab bar frame directly.

查看更多
登录 后发表回答