【问题标题】:How to switch between transparant UINavigationBar and UINavigationBar with backgroundColor如何在透明 UINavigationBar 和具有背景颜色的 UINavigationBar 之间切换
【发布时间】:2019-03-19 13:12:55
【问题描述】:

我有几个UIViewControllers,它们都有纯色背景。但是,在用户的个人资料上,我想在背景上显示一个模糊的个人资料图像,所以我需要一个透明的UINavigationBar。这很好用。我的做法如下:

我所有的UIViewControllers 都设置为具有透明的 UINavigationBar,如下所示:

    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.isTranslucent = true

然后我通过简单地创建一个矩形UIView 并将所有边缘固定到UINavigationBar 的位置来简单地添加一个“假”栏,从而获得相同的外观,但允许我让个人资料页面透明。但是,我现在有一种情况需要使用“真实”UINavigationBar,因为在我目前的方法中,当我显示键盘并为view.frame.origin.y 设置动画以使屏幕向上移动时,它会“超过”这个假UINavigationBar.

为此,我在此 viewController 中设置了 UINavigationBarbarTintColor 属性。当我第一次转到此视图然后转到个人资料页面时出现问题:它仍然是相同的纯色。直接启动后进入个人资料页面时不会出现此问题。

This is the desired result, which I can see when I go directly to a profile page.

This is what a normal viewController looks like, with the "fake" UINavigationBar

And this is the error. This happens when I first go to the viewController with the barTintColor set, and then to a profile page

关于如何解决此问题的任何提示?我已经尝试使用 barTintColor 设置“重置”viewController 的 viewWillDisappear 中的 UINavigationBar 属性,但这不起作用:

override func viewWillDisappear(_ animated: Bool) {

    super.viewWillDisappear(animated)
    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.isTranslucent = true
        navigationController?.navigationBar.barTintColor = nil

}

可能有用:我大部分时间都使用推送转换(肯定是在相关视图控制器之间)。仅使用 present 来显示弹出窗口或类似内容。一切都在代码中 - 没有故事板。

任何帮助将不胜感激!

【问题讨论】:

  • 快速思考 - 你试过继承UINavigationController吗?我出于完全不同的原因这样做(子类化)并且效果很好。也许它可以帮助你?

标签: ios swift uinavigationcontroller uinavigationbar


【解决方案1】:

您可以使用 Present 代替 Push。所以你可以有两个不同的导航栏。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多