Swift iOS - 导航栏不会保持隐藏状态,即使我隐藏了它

Swift iOS -Navigation Bar won't stay Hidden even though I'm hiding it

一切都以编程方式进行。没有故事板和集合视图的 vc 和详细的 vc 都在 TabBarController 中。

我正在使用集合视图,当我点击 didSelectItem 中的单元格时,我会按下详细视图控制器。在 DetailedVC 中,我隐藏了导航控制器。我分别和累积地在 viewDidLoadviewWillAppear 中调用了以下内容,以尝试隐藏它:

navigationController?.isNavigationBarHidden = true
navigationController?.navigationBar.isHidden = true
navigationController?.setNavigationBarHidden(true, animated: false)

当场景第一次出现时,导航栏是隐藏的。问题是当我在 DetailedVC 上向下滑动时,导航栏通过滑动从屏幕顶部向下滑动并且它不会消失。不小心往下滑发现的

我按了导航栏的后退按钮,它起作用了,尽管它应该被隐藏。我隐藏它的原因是因为我有一个视频在 DetailedVC 的最顶部播放,所以我使用自定义按钮弹出回集合视图。我还隐藏了状态栏(类似于 YouTube),但它保持隐藏状态。

DetailedVC 是一个常规视图控制器,它不包含 table 视图或集合视图,所以我很困惑为什么它允许我向下滑动以及为什么导航栏不会停留隐藏?

推送 DetailedVC 的集合视图单元格:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let detailVC = DetailController()
        navigationController?.pushViewController(detailVC, animated: true)
}

详细VC:

class DetailController: UIViewController {


    let customButton: UIButton = {
        let button = UIButton(type: .system)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle("< Back", for: .normal)
        button.setTitleColor(UIColor.orange, for: .normal)
        button.addTarget(self, action: #selector(handleCustomButton), for: .touchUpInside)
        return button
     }()

override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white
}

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        UIApplication.shared.isStatusBarHidden = true

        // I tried all of these individually and cumulatively and the nav still shows when I swipe down
        navigationController?.isNavigationBarHidden = true
        navigationController?.navigationBar.isHidden = true
        navigationController?.setNavigationBarHidden(true, animated: false)
}

override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        UIApplication.shared.isStatusBarHidden = false
}

@objc fileprivate func handleCustomButton()
        navigationController?.popViewController(animated: true)
}

@objc fileprivate func configureButtonAnchors()
        //customButton.leftAnchor...
}

我不确定为什么当我在 DetailVC 中向下滑动时,导航栏变得不隐藏,但我移动了代码以将其隐藏在 viewDidLayoutSubviews 中,现在它保持隐藏状态。

为了解决这个问题,我使用了navigationController?.setNavigationBarHidden(true, animated: false)并将其设置在viewDidLayoutSubviews:

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        // this one worked the best
        navigationController?.setNavigationBarHidden(true, animated: false)
}

并对其进行设置,使其可以显示在前一个 vc 中,这将是集合视图:

override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        navigationController?.setNavigationBarHidden(false, animated: false)
}

我说它效果最好,因为我分别尝试了所有 3 个,其中 3 个 navigationController?.navigationBar.isHidden = true 有问题。出于某种原因,即使在 viewDidLayoutSubviews 中,即使导航栏没有重新出现,它也会使 DetailedVC 上下颠簸。

并且 navigationController?.isNavigationBarHidden = true 在 DetailedVC 中工作,导航栏保持隐藏状态并且场景没有混乱但是当我在 viewWillDisappear 中将其设置为 false 以便导航栏显示在parent vc(集合视图)导航栏没有出现在那里。