首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >斯威夫特iOS -Navigation Bar不会隐藏,即使我在隐藏它

斯威夫特iOS -Navigation Bar不会隐藏,即使我在隐藏它
EN

Stack Overflow用户
提问于 2018-03-08 16:22:21
回答 1查看 2.1K关注 0票数 2

一切都是按计划进行的。没有故事板和集合视图的vc和详细的vc都在TabBarController中。

我使用的是集合视图,当我点击didSelectItem中的一个单元格时,我会按下一个详细的视图控制器。在DetailedVC中,我隐藏了导航控制器。我在viewDidLoadviewWillAppear中单独和累积地调用了下面的代码,试图隐藏它:

代码语言:javascript
运行
AI代码解释
复制
navigationController?.isNavigationBarHidden = true
navigationController?.navigationBar.isHidden = true
navigationController?.setNavigationBarHidden(true, animated: false)

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

我按下导航栏的后退按钮,即使它应该被隐藏,它也能工作。我之所以隐藏它,是因为我有一个在DetailedVC顶部播放的视频,所以我使用一个自定义按钮弹出集合视图。我也隐藏状态栏(类似于YouTube),但这是隐藏的。

DetailedVC是一个常规的视图控制器,它不包含表视图或集合视图,所以我不明白为什么它允许我向下滑动,为什么导航条不会隐藏?

将DetailedVC推上的集合视图单元格:

代码语言:javascript
运行
AI代码解释
复制
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let detailVC = DetailController()
        navigationController?.pushViewController(detailVC, animated: true)
}

DetailedVC:

代码语言:javascript
运行
AI代码解释
复制
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...
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-08 20:51:00

我不太清楚为什么当我在DetailVC内部滑动时,导航栏变得没有隐藏,但是我移动代码以将它隐藏在viewDidLayoutSubviews中,现在它仍然是隐藏的。

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

代码语言:javascript
运行
AI代码解释
复制
override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

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

并将其设置为可以在前面的vc中显示,这将是集合视图:

代码语言:javascript
运行
AI代码解释
复制
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 (集合视图)中,导航条不会出现在其中。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49184714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文