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

标签 ios swift uiviewcontroller uinavigationcontroller uinavigationbar

一切都以编程方式进行。没有 Storyboard和 Collection View 的 vc 和详细的 vc 都在 TabBarController 中。

我正在使用一个 Collection View ,当我点击 didSelectItem 中的一个单元格时,我会按下一个详细 View Controller 。在 DetailedVC 中,我隐藏了导航 Controller 。我在 viewDidLoadviewWillAppear 中单独和累积地调用了下面的代码,以尝试隐藏它:

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

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

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

DetailedVC 是一个常规 View Controller ,它不包含 TableView 或 Collection View ,所以我很困惑为什么它允许我向下滑动以及为什么导航栏不会保持隐藏状态?

推送 DetailedVC 的 Collection View 单元格:

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 中,这将是 Collection View :

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 时这样导航栏就会显示在父 vc( Collection View )内,导航栏不会出现在那里。

关于ios - Swift iOS - 即使我隐藏它,导航栏也不会保持隐藏状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49184714/

相关文章:

iphone - 全局声明使用 viewWithTag : and declaring in viewDidLoad:

ios - 滚动到包含某个 UITextField 的 UITableViewCell

iphone - 为什么会出现过度释放呢? UINavigationController/UITableview

ios - 自定义信息窗口上的按钮未在 ios 中接收操作

ios - Swift iOS,在 popToRootViewViewController 之后调用函数

iphone - 移动 tableview 时裁剪 UITableviewcell

ios - 当我以格式字符串调用NSLog并进一步使用该时间时,如何获得时间?

swift - 切换披露三角形?

ios - 如何在 TableView 部分使用一个带有可选用例的枚举

arrays - 从值字典数组中检索键