返回 UINavigationController 时如何隐藏 UITabBar
How to hide UITabBar when going back in UINavigationController
我有三个视图控制器:
- FeedController(
UITabBar
可见)
- Post控制器(
UITabBar
已隐藏)
- UserController(
UITabBar
可见
我使用以下代码执行此操作,从 FeedController 到 PostController:
let postVC = PostController()
postVC.hidesBottomBarWhenPushed = true
pushViewController(postVC, animated: true)
postVC.hidesBottomBarWhenPushed = false
然后,从PostVC到用户VC:
let userVC = UserController()
userVC.hidesBottomBarWhenPushed = false
pushViewController(userVC, animated: true)
效果很好。除了导航到 Post 时,它到处都显示 UITabBar
。但是,当我从 Post 中转到用户配置文件 (UserController) 时会出现问题。它按预期在个人资料上显示了 UITabBar
,但是当我向后导航(使用 UINavigationController
中的后退按钮)时,UITabBar
仍然可见。我希望当我从用户VC返回到帖子VC时再次隐藏它。
有什么方法可以做到这一点吗?
试试你的 post viewController:
override func viewWillDisappear(_ animated: Bool) {
postVC.hidesBottomBarWhenPushed = true
}
这将在视图即将消失时调用它,而不是在它出现时调用它,因此当您返回时它应该隐藏。
我有三个视图控制器:
- FeedController(
UITabBar
可见) - Post控制器(
UITabBar
已隐藏) - UserController(
UITabBar
可见
我使用以下代码执行此操作,从 FeedController 到 PostController:
let postVC = PostController()
postVC.hidesBottomBarWhenPushed = true
pushViewController(postVC, animated: true)
postVC.hidesBottomBarWhenPushed = false
然后,从PostVC到用户VC:
let userVC = UserController()
userVC.hidesBottomBarWhenPushed = false
pushViewController(userVC, animated: true)
效果很好。除了导航到 Post 时,它到处都显示 UITabBar
。但是,当我从 Post 中转到用户配置文件 (UserController) 时会出现问题。它按预期在个人资料上显示了 UITabBar
,但是当我向后导航(使用 UINavigationController
中的后退按钮)时,UITabBar
仍然可见。我希望当我从用户VC返回到帖子VC时再次隐藏它。
有什么方法可以做到这一点吗?
试试你的 post viewController:
override func viewWillDisappear(_ animated: Bool) {
postVC.hidesBottomBarWhenPushed = true
}
这将在视图即将消失时调用它,而不是在它出现时调用它,因此当您返回时它应该隐藏。