swift 当推/回时隐藏/显示标签栏,雨燕

nue99wik  于 2023-08-02  发布在  Swift
关注(0)|答案(7)|浏览(206)

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

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

字符串
我想要
视图控制器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


如果1 -> 3,则返回到1,有效。
如果1 -> 3 -> 2,则返回到3并返回到1,工作。
如果2 -> 4,则返回2,工作。
如果1 -> 3 -> 2 -> 4,则返回到2,标签栏不显示。想知道为什么。有什么建议或解释隐藏底部酒吧时推,因为它让我困惑了很多
x1c 0d1x的数据

uemypmqf

uemypmqf1#

顾名思义,hiddenBottomBarWhenPushed只在需要时隐藏底部栏,它不会取消隐藏底部栏。你可以这样做来让它工作:

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

字符串
或者将self.tabBarController?.tabBar.hidden = true/false放入prepareForSegue

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

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

cgfeq70w

cgfeq70w2#

将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)

字符串

kjthegm6

kjthegm63#

这是我的两分钱。Swift 3/4/5:
方法1:(推荐)

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.
    }
}

字符串
方法二:

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
}

whlutmcx

whlutmcx4#

在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
    }
}

字符串

0yg35tkg

0yg35tkg5#

Swift 5

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    self.hidesBottomBarWhenPushed = true
}

字符串

x9ybnkn6

x9ybnkn66#

It's Better to Fade Out than to Hide Away
(....我的,嘿嘿)
Swift 5:
另一种方法…是淡入淡出选项卡栏。并不是说这在战略上比其他任何一个更有利,比如那些需要prepareForSegue的,但它可以适应其他触发器。在任何情况下,设置选项卡栏Alpha动画可以避免在tabBar(UIView)上设置.isHidden时产生的刺耳的消失/出现效果,方法是淡入淡出。这是在任何VC中完成的,当VC被推入或加载时需要隐藏它,当VC被弹出或卸载时需要取消隐藏。
这将不会打扰恢复标签栏,直到在navBar或等效的操作中按下后退按钮,这样当一个子VC被推到标签栏上时,标签栏将保持隐藏,这通常(但并不总是)是有意义的。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    UIView.animate(withDuration: 0.4, delay: 0.0, options: UIView.AnimationOptions.curveEaseOut, animations: {
        self.tabBarController?.tabBar.alpha = 0.0
    }, completion: { (finished: Bool) -> Void in
        self.tabBarController?.tabBar.isUserInteractionEnabled = false

    })
}

override func viewWillDisappear(_ animated: Bool) {
    if self.isMovingFromParent {
        UIView.animate(withDuration: 0.4, delay: 0.0, options: UIView.AnimationOptions.curveEaseOut, animations: {
            self.tabBarController?.tabBar.alpha = 1.0
        }, completion: { (finished: Bool) -> Void in
            self.tabBarController?.tabBar.isUserInteractionEnabled = true
        })
    }
}

字符串

u0njafvf

u0njafvf7#

在情节提要中,选择“推送时隐藏底部栏”

相关问题