【问题标题】:show/hide the tabbar when needed from a view controller在需要时从视图控制器显示/隐藏标签栏
【发布时间】:2012-02-06 11:24:33
【问题描述】:

我是 iOS 编程新手。我真的需要你的帮助。

我有一个登录屏幕,可将我带到地图(谷歌 API)。单击任何创建的注释时,我想加载一个带有 2 个视图的标签栏。

我搜索并发现我需要在开始时添加标签栏,即 appdelegate 并在需要时显示/隐藏标签栏。

所以我做了 2 个函数来显示和隐藏标签栏

-(void)Load_tabBar{
[self.navigationController.view removeFromSuperview];
[self.window addSubview:tabBarController.view];
[self.window makeKeyWindow];}

-(void)remove_tabBar{
self.tabBarController.selectedIndex=0;
[self.tabBarController.view removeFromSuperview];
[self.window addSubview:navigationController.view];
[self.window makeKeyWindow];}

当我调用 Load_tabBar 方法时它确实有效,当我点击返回时它调用 remove_tabBar 方法。如果我再次调用 Load_tabBar 方法并返回,它会崩溃并给出错误

-[UILayoutContainerView 窗口]:消息发送到已释放实例 0x563b0b0

已编辑:PS:我可以将标签栏视图添加到视图控制器然后推送该视图吗?

thnx

【问题讨论】:

    标签: ios xcode ipad


    【解决方案1】:

    使用这个self.hidesBottomBarWhenPushed = YES;

    【讨论】:

    • 我可以将标签栏视图添加到视图控制器然后推送该视图吗?
    • 当您在该呈现视图控制器中推送视图控制器时,您必须在 viewWillAppear 方法中添加它。无需使用您的代码
    • 最好在前一个视图控制器的 prepareForSegue 方法中使用它。
    【解决方案2】:

    这种方法绝对有效。你只需要在 push 之前把它放在方法中,像这样:

    -actionThatPushTheViewController { 
    
        //create the view controller here, do some init. 
    
        //then:
        theViewControllerToBePushed.hidesBottomBarWhenPushed = YES;
    
        //push it here like this: 
        [self.navigationController pushViewController:theViewControllerToBePushed animated:YES];
    

    【讨论】:

      【解决方案3】:

      希望这两种方法对你有帮助,

      - (void) hideTabBar:(UITabBarController *) tabbarcontroller {
      
      int height = 480;
      
      [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationDuration:0.3];
      
      for(UIView *view in tabbarcontroller.view.subviews) {
          if([view isKindOfClass:[UITabBar class]]) {
              [view setFrame:CGRectMake(view.frame.origin.x, height, view.frame.size.width, view.frame.size.height)];
          } 
          else {
              [view setFrame:CGRectMake(view.frame.origin.x,view.frame.origin.y, 320, 436)];
          }
      }
      [UIView commitAnimations];
      

      }

      - (void) showTabBar:(UITabBarController *) tabbarcontroller {
      
      int height = 480;
      
      [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationDuration:0.3]; 
      
      for(UIView *view in tabbarcontroller.view.subviews) {
      
          if([view isKindOfClass:[UITabBar class]]) {
              [view setFrame:CGRectMake(view.frame.origin.x, height, view.frame.size.width, view.frame.size.height)];            
          } 
          else {
              [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, height)];
          }
      }    
      
      [UIView commitAnimations];
      

      }

      只需将这两个方法放在 AppDelegate 类中,然后根据您的要求在需要的地方调用它。

      【讨论】:

      • 我不想改变子视图的帧大小..弹出时,它完全弹出到另一个视图..子视图也消失了
      • 似乎运行良好,我添加了一些调整来处理 iPhone 和 iPad。我得到窗口的宽度和高度,就像这样 float height = self.window.frame.size.height;浮动宽度 = self.window.frame.size.width;
      【解决方案4】:

      如果你想在推送时隐藏它并在弹出时显示它是代码:

      if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondVC") as? SecondVC {
          if let navigator = navigationController {
              viewController.hidesBottomBarWhenPushed = true
              navigator.pushViewController(viewController, animated: true)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-03
        • 2023-03-27
        相关资源
        最近更新 更多