【问题标题】:Unable to change navigation bar color无法更改导航栏颜色
【发布时间】:2016-06-03 06:34:52
【问题描述】:

我正在尝试在包含 UISearchController 的视图中更改导航栏的颜色。我之前在我的 appDelegate 中为整个应用程序设置了导航栏的颜色,但我希望这个视图导航栏具有不同的颜色。问题是我不知道放置代码的哪个函数会覆盖 appDelegate 代码。例如,viewDidLoad 和 viewWilAppear 在视图第一次加载时不会改变颜色,只有在我进入并取消 searchController 之后。我应该放置以下哪个函数?

UINavigationBar.appearance().backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)

【问题讨论】:

  • 在viewDidLayoutSubviews()中试试
  • 没用@GuyDaher

标签: ios swift uinavigationbar background-color appdelegate


【解决方案1】:

有两种方法可以做到这一点。您可以修改在 AppDelegate 中为整个应用设置的外观代理,也可以修改特定屏幕的单个导航栏。

当您关闭视图时 - 您需要在 viewWillDisappear 中重置 barTintColor。

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

    // Set to new colour for whole app
    UINavigationBar.appearance().barTintColor = UIColor.blueColor()

    // Or, Set to new colour for just this navigation bar
    self.navigationController?.navigationBar.barTintColor = UIColor.blueColor()
}

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

    //Revert to old colour, whole app
    UINavigationBar.appearance().barTintColor = UIColor.redColor()

    //Revert to old colour, just this navigation bar
    self.navigationController?.navigationBar.barTintColor = UIColor.redColor()
}

【讨论】:

    【解决方案2】:

    您应该在viewWillAppear 中更改导航控制器的栏颜色,并在关闭视图时将其改回,而不是直接更改导航栏颜色。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 2014-09-14
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多