ios - 如果通过 segue 从另一个选项卡进入,如何从 Controller 中删除 TabBar?

标签 ios swift uiviewcontroller uinavigationcontroller uitabbarcontroller

我正在使用一个包含 5 个选项卡的选项卡栏 Controller 。在 tab1 中,我有一个按钮可以将我带到 tab2。该 tab2 嵌入在导航 Controller 中。

那么当我通过segue从tab1来时,如何使Tab栏隐藏在tab2中?

在 Storyboard 中,我将Hide Bottom bar on Push激活。另外,我在 View 中编写了 self.tabBarController?.tabBar.isHidden = true 加载了 tab2。 在tab1中我的prepareForSegue是这样的

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "ShortcutSegue" {

        let tabVc = segue.destination as! UITabBarController
        tabVc.selectedIndex = 1
        tabVc.tabBarController?.tabBar.isHidden = true
    }         
}

最佳答案

对于 tab2 View Controller ,您可以编写以下代码来隐藏选项卡栏。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  if let destinationTabBar = segue.destinationViewController as? UITabBarController {
    if segue.identifier == "ShortcutSegue" {
      destinationTabBar.viewControllers?.removeAtIndex(adminScreenIndex)
    }
  }
}

或者您可以在您的重写函数prepareForSegue 方法中编写以下代码。

if let tabVc = segue.destinationViewController as? tab2ViewController {
  tabVc.hidesBottomBarWhenPushed = true
}

或者您可以在属性检查器的主 Storyboard 中为选项卡栏 View Controller 勾选“按下时隐藏按钮栏”,如下图所示。

Main storyboard attribute inspector "Hide Button Bar On Push"

关于ios - 如果通过 segue 从另一个选项卡进入,如何从 Controller 中删除 TabBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45194521/

相关文章:

objective-c - 将渐变放在 tableview 单元格背景上

swift - 如何编写需要将属性表示为集合以及属性的数据模型

iPhone - 包括 subview 和可能的内存泄漏?

iphone - UISwitch 设置为 ON,它不显示模态视图 Controller 转换

iphone - willAnimateToRotation 没有被调用

ios - 在 iTunes 上上传构建时捆绑操作系统类型代码无效

ios - iOS (Swift) 应用程序中的 AWS Cognito 用户池

ios - LLDB 等同于 GDB 的 "info malloc-history <address>"命令?

ios - 检查 View Controller 是否通过动画成功解除

ios - 如何将最后一行添加到 UITableView 并平滑滚动?