iOS 在将 hidesBottomBarWhenPushed 设置为 true 的情况下弹出视图控制器后不显示选项卡栏

问题描述 投票:0回答:1

我对

hidesBottomBarWhenPushed
有疑问。

按下(隐藏选项卡栏)和弹出(显示选项卡栏)时它可以正常工作。但我有这个小宝石:

  • 有一个 UITabBarController,它有一个 UINavigationController 作为选项卡,其中有一个 UIViewController
  • 将一个新的 UIViewController 压入堆栈,该视图控制器在 init 中将
    hidesBottomBarWhenPushed
    设置为 true。标签栏正确隐藏
  • 此视图中有一个按钮可以执行两个操作
    • 以模态方式呈现一个新的视图控制器(全屏,用于工作表演示)
    • 弹出当前视图控制器
  • 关闭模态呈现的视图
  • 初始 UINavigationController 与其子级一起显示,但是选项卡栏仍然隐藏

遗憾的是,由于没有官方函数可以调用 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)
        }))
    }
}
ios uikit uinavigationcontroller uitabbarcontroller
1个回答
0
投票

似乎“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
}

对我来说,这没有任何视觉伪影,但它也有点老套,这始终是一个有问题的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.