首页 > 解决方案 > hidesBottomBarWhenPushed 永远隐藏 TabBar

问题描述

我使用 Xcode 11.2,项目最低 iOS 部署目标是 iOS 12.4。

我在根页面上有一个 TabBarController,在我有 FirstViewController 的一个选项卡上。当我从 FirstViewController 推送 SecondViewController 时,我希望隐藏标签栏。我使用 hidesBottomBarWhenPushed 属性来隐藏标签栏。

当我按下 SecondViewController 时标签栏是隐藏的,但是当我弹出 SecondViewController 并移回 FirstViewController 时,标签栏仍然是隐藏的。

回到 FirstViewController 时,我尝试了几种方法将 hidesBottomBarWhenPushed 设置为 false,但没有一种尝试有效。

弹出回 FirstViewController 时如何重新显示选项卡栏?

class FirstViewController: UIViewController {

    @IBAction func buttonTap(_ sender: Any) {
        let vc2 = SecondViewController()

        // Set to Hide TabBar
        hidesBottomBarWhenPushed = true

        navigationController?.pushViewController(vc2, animated: true)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // This Does Not Work
        hidesBottomBarWhenPushed = false
    }
}


class SecondViewController: UIViewController {

    /*
        All The Followings Does Not Work
    */

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        hidesBottomBarWhenPushed = false
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        hidesBottomBarWhenPushed = false
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)

        hidesBottomBarWhenPushed = false
    }
}

标签: iosswiftuitabbarcontrolleruitabbar

解决方案


关键是从 SecondViewController 外部将 hidesBottomBarWhenPushed 设置为 true。

下面的代码就是我需要编写的全部内容。

class FirstViewController {

    func pushSecondViewController {
        let vc = SecondViewController()
        vc.hidesBottomBarWhenPushed = true // <- Here
        navigationController?.push
        navigationController?.pushViewController(vc, animated: true)
    }
}

推荐阅读