推/退时隐藏/显示标签栏。 swift
hide / show tab bar when push / back. swift
回答:
在每个视图控制器中使用 self.tabBarController?.tabBar.hidden 而不是 hidesBottomBarWhenPushed 来管理视图控制器是否应显示标签栏。
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,不显示标签栏。想知道为什么。 hidesBottomBarWhenPushed 的任何建议或解释,因为它让我很困惑
顾名思义,hiddenBottomBarWhenPushed 仅在需要时隐藏底部栏,不会取消隐藏底部栏。
你可以这样做来让它工作:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.hidden = true/false
}
或者简单地将 self.tabBarController?.tabBar.hidden = true/false
放入 prepareForSegue
但我不建议你这样做,因为如果bottomBar突然弹出会很奇怪,用户会认为他们突然回到了rootViewController,而实际上没有。
Users should always know where they are in your app and how to get to their next destination.
在 ViewController 您想要 pushed/popped 上的 hide/show 标签栏中添加此实现。它也适用于所有下一个推送的视图控制器。
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
}
}
将 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)
这是我的两分钱。
Swift 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.
}
}
方法二:
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
}
Swift 5
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
self.hidesBottomBarWhenPushed = true
}
Swift 5:
还有另一种方法...使 tabBar 淡入和淡出..并不是说这在战略上比任何其他方法更具优势,例如需要 prepareForSegue
的方法,但可以适应它。在任何情况下,通过淡入淡出在 tabBar (UIView) 上设置 .isHidden 时,动画标签栏 alpha 避免了严酷的 disappear/appear 效果。这是在任何 VC 中完成的,当 VC 被推入或加载时需要它隐藏,而当 VC 被弹出或卸载时需要它被隐藏。
这不会费心恢复标签栏,直到在导航栏中按下后退按钮或等效操作,这样当子 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
})
}
}
回答: 在每个视图控制器中使用 self.tabBarController?.tabBar.hidden 而不是 hidesBottomBarWhenPushed 来管理视图控制器是否应显示标签栏。
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,不显示标签栏。想知道为什么。 hidesBottomBarWhenPushed 的任何建议或解释,因为它让我很困惑
顾名思义,hiddenBottomBarWhenPushed 仅在需要时隐藏底部栏,不会取消隐藏底部栏。 你可以这样做来让它工作:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.hidden = true/false
}
或者简单地将 self.tabBarController?.tabBar.hidden = true/false
放入 prepareForSegue
但我不建议你这样做,因为如果bottomBar突然弹出会很奇怪,用户会认为他们突然回到了rootViewController,而实际上没有。
Users should always know where they are in your app and how to get to their next destination.
在 ViewController 您想要 pushed/popped 上的 hide/show 标签栏中添加此实现。它也适用于所有下一个推送的视图控制器。
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
}
}
将 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)
这是我的两分钱。 Swift 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.
}
}
方法二:
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
}
Swift 5
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
self.hidesBottomBarWhenPushed = true
}
Swift 5:
还有另一种方法...使 tabBar 淡入和淡出..并不是说这在战略上比任何其他方法更具优势,例如需要 prepareForSegue
的方法,但可以适应它。在任何情况下,通过淡入淡出在 tabBar (UIView) 上设置 .isHidden 时,动画标签栏 alpha 避免了严酷的 disappear/appear 效果。这是在任何 VC 中完成的,当 VC 被推入或加载时需要它隐藏,而当 VC 被弹出或卸载时需要它被隐藏。
这不会费心恢复标签栏,直到在导航栏中按下后退按钮或等效操作,这样当子 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
})
}
}