【问题标题】:How I can remove navigation bar background with appearance in the iOS 13?如何在 iOS 13 中删除带有外观的导航栏背景?
【发布时间】:2019-10-26 16:17:15
【问题描述】:

我尝试在带有#available 的if 语句中删除iOS 13 中的导航栏背景。我知道在 else 的语句中删除 iOS 12 和旧 iOS 的导航栏背景的原始代码。不过,Apple 确实在任何地方宣布了一个名为 Appearance 的新系统,以支持新的 iOS 13 系统。

    let app = UINavigationBarAppearance()

    let navigationBar = self.navigationController?.navigationBar

    app.configureWithOpaqueBackground()
    app.shadowImage = UIImage()


    self.navigationController?.navigationBar.scrollEdgeAppearance = app

    navigationBar!.standardAppearance = app
    navigationBar!.scrollEdgeAppearance = app

我相信这个 configureWithOpaqueBackground() 可以让我们移除导航栏背景,但我在 iOS 13.1 模拟器上测试出现黑色导航栏背景。我知道是什么原因造成的。

     app.configureWithOpaqueBackground()
     app.titleTextAttributes = [.foregroundColor: UIColor.white]
     app.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
     app.backgroundColor = #colorLiteral(red: 0.1603881121, green: 0.1677560508, blue: 0.2133775949, alpha: 1)

该代码允许我们将自定义颜色放在黑色背景上。我准备在 viewWillDisappear 的语句中添加该代码,以将导航栏背景恢复为self.navigationController?.navigationBar.scrollEdgeAppearance = app 之前的正常颜色背景,并删除app.configureWithOpaqueBackground()app.shadowImage = UIImage()。现在,我需要在viewWillAppear的语句中创建半透明的导航栏背景,但是由于仍然显示黑色背景,它看不到任何半透明的背景。

非常感谢您帮助解决问题! :)

【问题讨论】:

  • 我不理解目标。您是否希望导航的背景在所有情况下都清晰(完全透明且不可见)?是不是你总是希望它是白色的?什么?
  • 这就是我想要明确的导航背景之一。我很抱歉这篇文章让你有点困惑。我还在学习英语。

标签: ios swift uinavigationbar ios13


【解决方案1】:

如果你想让导航栏变得完全透明:

    let app = UINavigationBarAppearance()
    app.configureWithTransparentBackground()
    self.navigationController?.navigationBar.standardAppearance = app
    self.navigationController?.navigationBar.scrollEdgeAppearance = app
    self.navigationController?.navigationBar.compactAppearance = app

不要乱用导航栏的isTranslucent

【讨论】:

  • 宾果游戏!顺便说一句:我在错误的图层上上传了 UIImage。现在,它是完美的透明!非常感谢您帮助解决问题! :D
  • 我不确定它是否总是必要的,但是直到我在设置standardAppearance等之后添加了一个navigationController.setNeedsLayout()才生效。我们的导航控制器在一个UISplitViewController和上面的代码在 viewDidLoad 方法中,所以它可能是它的一部分。
  • @LenK 谢谢,这是一个很有价值的评论。是的,我在让布局在拆分视图控制器中正常运行时也遇到了困难,所以这很可能与它有关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-14
相关资源
最近更新 更多