我对
hidesBottomBarWhenPushed
有疑问。
按下(隐藏选项卡栏)和弹出(显示选项卡栏)时它可以正常工作。但我有这个小宝石:
hidesBottomBarWhenPushed
设置为 true。标签栏正确隐藏遗憾的是,由于没有官方函数可以调用 UITabBarController 来隐藏或显示 UITabBar,我没有看到解决此问题的好方法。也没有重新评估当前状态的功能。
如果演示稍微延迟,一切正常(但有些利益相关者不希望这样......)
这是重现该错误的完整代码(假设一个带有故事板的新项目,其中有一个 UITabBarController 作为入口点,其中有一个 UINavigationController ,其中
ViewController
作为其子项)
请原谅“丑陋”,这只是简单的演示代码
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
tabBarController?.tabBar.isTranslucent = false
view.backgroundColor = .red
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.navigationController?.pushViewController(VC2(), animated: true)
}
}
}
class VC2: UIViewController {
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
hidesBottomBarWhenPushed = true
navigationItem.leftBarButtonItem = UIBarButtonItem(systemItem: .close, primaryAction: UIAction(handler: { _ in
let nvc = self.navigationController
let vc = UINavigationController(rootViewController: VC3())
vc.modalPresentationStyle = .fullScreen
// With a delay it all works fine
// DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
nvc?.present(vc, animated: true)
// }
self.navigationController?.popViewController(animated: true)
}), menu: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .blue
}
}
class VC3: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
navigationItem.leftBarButtonItem = UIBarButtonItem(systemItem: .close, primaryAction: UIAction(handler: { _ in
self.dismiss(animated: true)
}))
}
}
似乎“hacky”解决方案是对“拥有的”UINavigationController 进行修改
override public func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Determine if we are in the invalid state
guard
let tabBarController,
tabBarController.tabBar.isHidden,
let last = children.last,
!last.hidesBottomBarWhenPushed
else {
return
}
// Force the navigation controller to reevaluate the current `hidesBottomBarWhenPushed`
pushViewController(UIViewController(), animated: false)
popViewController(animated: false)
// The safe space is shown but the tab bar is still set to hidden
tabBarController.tabBar.isHidden = false
}
对我来说,这没有任何视觉伪影,但它也有点老套,这始终是一个有问题的解决方案。