【问题标题】:Hide tab bar in IOS swift app在 IOS swift 应用程序中隐藏标签栏
【发布时间】:2015-04-30 22:11:57
【问题描述】:

我试图弄清楚如何在我的 iOS swift 应用程序中隐藏标签栏。我不关心任何花哨的动画或任何东西。我可以在 ViewDidLoad() 函数中添加一些内容。

【问题讨论】:

  • 在推送 VC 之前设置此项。 vc.hidesBottomBarWhenPushed = true 应该做这项工作。不要手动显示和隐藏标签栏。

标签: ios swift


【解决方案1】:

您可以在 ViewDidLoad() 方法中简单地使用它。

self.tabBarController?.tabBar.hidden = true

对于 Swift 3.0、4.0、5.0

self.tabBarController?.tabBar.isHidden = true

或者你可以这样改变标签栏的z位置:

self.tabBarController?.tabBar.layer.zPosition = -1

如果你想再次显示它:

self.tabBarController?.tabBar.layer.zPosition = 0

【讨论】:

  • Dharmesh,感谢您的回复。这成功地隐藏了标签栏,但是表格视图没有填满屏幕。标签栏应该是黑色的。我想还有另一个步骤让它适合屏幕?
  • 为此添加约束。
  • 要添加哪些“约束”?
  • 如果你使用了约束,那么你可以改变它的高度约束,或者你可以改变tableView的框架。 @db0
  • 但标签栏仍然会被触摸,请尝试在不可见标签栏的任何标签附近点击。 @DharmeshKheni
【解决方案2】:

推送前设置controller.hidesBottomBarWhenPushed = true

let objCreateEventVC = CreateEventVC()
objCreateEventVC.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(objCreateEventVC, animated: false)

【讨论】:

  • 使用setViewControllers添加一堆控制器时不起作用
【解决方案3】:

接受的答案有效,但到其他视图的过渡有一个断断续续的动画(标签栏动画)

还想补充一点,虽然 Kalpesh 的解决方案对我来说非常有效,但我发现每个视图控制器都有一个 hidesBottomBarWhenPushed 属性(查看故事板。)如果你想隐藏标签栏,你应该勾选它。而且效果很好。

更新:我不确定这是否是已知的事情,但这是苹果文档页面所说的:

A view controller added as a child of a navigation controller can display an optional toolbar at the bottom of the screen. The value of this property on the topmost view controller determines whether the toolbar is visible. If the value of this property is true, the toolbar is hidden. If the value of this property is false, the bar is visible.

我认为这意味着您必须在最顶层的视图控制器(导航堆栈上的第一个)设置 hidesBottomBarWhenPushed 的基本值。一旦将其设置为 true,您可以将另一个更改为 false 或 true堆栈上的视图控制器。但是,如果您最顶层的视图控制器的 hidesBottomBarWhenPushed 值为 false,它将不会为导航堆栈上的其他控制器显示选项卡栏。

【讨论】:

  • 这是我们应该做的。如果您只想在呈现 ViewController 时隐藏它,这比其他答案要好得多
  • 为什么这在 Swift 4 中不起作用?我已经勾选了这个选项,但是当我转到那个特定的视图控制器时它仍然出现
  • 这也隐藏了在此之后添加到堆栈中的任何视图控制器的选项卡栏。有没有办法为那些后续的视图控制器恢复它?
  • @JustinVallely 请让我知道更新是否在答案中有效
  • 谢谢,接受的答案对我来说有问题,当我回到我来自的视图时,那个标签栏也消失了!
【解决方案4】:

斯威夫特 3.

self.tabBarController?.tabBar.isHidden = true

【讨论】:

    【解决方案5】:

    无需设置tabBar 的isHidden 属性。

    简单地说,转到 ViewController(在 StoryBoard 中)-> 属性检查器 -> 在“视图控制器”部分下选择“在推送时隐藏底栏”复选框。这就像一个魅力。

    如果你走'isHidden'方式,你需要做很多处理,即在你返回时让它再次出现,并在隐藏tabBar后删除底部的空白空间。

    【讨论】:

    • 这应该是实际的答案。这让我可以毫不费力地为我的 UI 元素设置约束。
    • Hide Bottom Bar on Push 有一个令人不快的动画,imo。
    • 当您从该页面返回时,Tabbar 会冻结一点然后返回。你觉得是什么原因?
    • 这在应用来自后台时不起作用。有什么想法吗?
    【解决方案6】:

    您也可以在扩展中设置它(使用 Dharmesh Kheni 答案)

    extension UITabBar {
    func tabsVisiblty(_ isVisiblty: Bool = true){
        if isVisiblty {
            self.isHidden = false
            self.layer.zPosition = 0
        } else {
            self.isHidden = true
            self.layer.zPosition = -1
        }
    }
    

    【讨论】:

      【解决方案7】:

      这是 Swift 4.0、4.1、4.2、5.0 及更高版本的编程方式

      tabBarController?.hidesBottomBarWhenPushed = true
      

      hidesBottomBarWhenPushed = true
      

      【讨论】:

      • @NicoS。真的。您在问题/答案中的什么地方读到了有关工具栏的内容?
      • 来自文档:一个布尔值,指示当视图控制器被推到导航控制器时屏幕底部的工具栏是否隐藏。
      • 问题是关于 TabBar 而不是 ToolBar
      • 第一个不起作用,第二个应该在执行 segue 时或在推送视图控制器之前添加。
      • 感谢:destinationVC.hidesBottomBarWhenPushed = true。对于所有其他选项,底部栏消失了,但那里有一个大黑洞。
      【解决方案8】:

      为了隐藏 navigationBar 和 tabBar,我使用了下一个函数:

      var tabBarHeight : CGFloat!
      
      func fullScreenAction(){
          if navigationController?.isNavigationBarHidden ?? false {
              //Show navigationBar
              navigationController?.setNavigationBarHidden(false, animated: false)
      
              //Show tabBar
              tabBarController?.tabBar.isHidden = false
              //Update the height of tabBar
              if (!(tabBarController?.tabBar.frame.size.height.isEqual(to: 0))!) {
                  tabBarHeight = self.tabBarController?.tabBar.frame.size.height
              }
              tabBarController?.tabBar.frame.size.height   = tabBarHeight
          } else {
              //Hide navigationBar
              navigationController?.setNavigationBarHidden(true, animated: false)
      
              //Hide tabBar
              tabBarController?.tabBar.isHidden = true
              //Update the height of tabBar
              tabBarHeight = tabBarController?.tabBar.frame.size.height
              tabBarController?.tabBar.frame.size.height   = 0
      
          }
      
      }
      

      当屏幕方向改变时tabBar的高度也改变了,所以我使用下一个函数退出全屏来调整高度:

      override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
          super.viewWillTransition(to: size, with: coordinator)
          if navigationController?.isNavigationBarHidden ?? false {
              navigationController?.setNavigationBarHidden(false, animated: false)
              tabBarController?.tabBar.isHidden = false
          }
      }
      

      希望对你有用。

      【讨论】:

        【解决方案9】:

        这是我的代码。它只是为了隐藏它的标签栏。 (如果没有很好地建立框架,底部会有一个黑色的视图。)

        var oldTabbarFr: CGRect = .zero
        
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
            oldTabbarFr = self.tabBarController?.tabBar.frame ?? .zero
        }
        
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            self.tabBarController?.tabBar.isHidden = true
            self.tabBarController?.tabBar.frame = .zero
        }
        
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            self.tabBarController?.tabBar.isHidden = false
            self.tabBarController?.tabBar.frame = oldTabbarFr
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-12-31
          • 1970-01-01
          • 2022-11-12
          • 1970-01-01
          • 2014-08-05
          • 1970-01-01
          • 2019-02-15
          相关资源
          最近更新 更多