按下/后退时隐藏/显示标签栏.迅速

Pak*_*ung 6 hide tabbar uiviewcontroller segue swift

答案:在每个视图控制器中使用self.tabBarController?.tabBar.hidden而不是hidesBottomBarWhenPushed来管理视图控制器是否应显示标签栏.

override func viewWillAppear(animated: Bool) {
    self.tabBarController?.tabBar.hidden = true/false
} 
Run Code Online (Sandbox Code Playgroud)

我想要

视图控制器1:应显示标签栏

视图控制器2:应显示标签栏

视图控制器3:不应显示标签栏.

视图控制器4:不应显示标签栏.

我写

// prepareForSegue in view controller 1, 
    let upcoming = segue.destinationViewController as! viewcontroller3
    upcoming.hidesBottomBarWhenPushed = true

// in view controller 3,
    func clickOnButton(button: UIButton) {
        self.hidesBottomBarWhenPushed = false
        self.performSegueWithIdentifier("viewController2", sender: self)
        self.hidesBottomBarWhenPushed = true
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "viewController2" {
            let upcoming = segue.destinationViewController as! viewController2
            upcoming.hidesBottomBarWhenPushed = false
        }
    }
// prepareForSegue in view controller 2
    let upcoming = segue.destinationViewController as! viewController4
    upcoming.hidesBottomBarWhenPushed = true
Run Code Online (Sandbox Code Playgroud)

如果1 - > 3然后回到1,工作.

如果1 - > 3 - > 2然后回到3并返回1,则有效.

如果2 - > 4,然后回到2,工作.

如果1 - > 3 - > 2 - > 4然后回到2,则不显示标签栏.想知道为什么.任何建议或对hidesBottomBar的一些解释,因为它让我很困惑

在此输入图像描述

Nic*_*len 27

正如它的名字所示,hiddenBottomBarWhenPush只在需要时隐藏底栏,它不会取消隐藏bottomBar.你可以这样做,让它工作:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.tabBarController?.tabBar.hidden = true/false
} 
Run Code Online (Sandbox Code Playgroud)

或者干脆把self.tabBarController?.tabBar.hidden = true/false在prepareForSegue

但我不建议你这样做,因为如果bottomBar突然弹出会很奇怪,用户会认为他们突然回到rootViewController而不是.

用户应始终知道他们在您的应用中的位置以及如何到达下一个目的地.

  • 对于 swift 4 : isHidden (2认同)

Sat*_*ync 7

这是我的两分钱。 斯威夫特 3/4/5:

方法一:(推荐)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "YourSegueIdentifier" {
        let destinationController = segue.destinationViewController as! YourViewController
        destinationController.hidesBottomBarWhenPushed = true // Does all the hide/show work.
    }
}
Run Code Online (Sandbox Code Playgroud)

方法二:

override func viewWillAppear(_ animated: Bool) { // As soon as vc appears
    super.viewWillAppear(true)
    self.tabBarController?.tabBar.isHidden = false
}

override func viewWillDisappear(_ animated: Bool) { // As soon as vc disappears
    super.viewWillDisappear(true)
    self.tabBarController?.tabBar.isHidden = true
}
Run Code Online (Sandbox Code Playgroud)


Sho*_*aib 5

在 ViewController 中添加此实现,您想要在推送/弹出时隐藏/显示选项卡栏。它也适用于所有下一个推送的视图控制器。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if wilmove {
        hidesBottomBarWhenPushed = true
    }
    wilmove = false
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if wilmove {
        hidesBottomBarWhenPushed = false
    }
    wilmove = false
}

var wilmove = false
override func willMove(toParentViewController parent: UIViewController?) {
    super.willMove(toParentViewController: parent)
    wilmove = true
    if !isViewLoaded {
        hidesBottomBarWhenPushed = true
    }
}
Run Code Online (Sandbox Code Playgroud)


nja*_*nja 5

将 hidesBottomBarWhenPushed 属性添加到目标视图控制器,并设置为 true。

带有标识符的推送 VC 示例:

    let storyboard = UIStoryboard(name: STORYBOARD_NAME, bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: VC_IDENTIFIER) as! YourViewController
    vc.hidesBottomBarWhenPushed = true
    navigationController?.pushViewController(vc, animated: true)
Run Code Online (Sandbox Code Playgroud)