0

当我回到我的 main 时,为什么 navigationBar 标题没有将其颜色变为白色UIViewController?这是简单的代码(viewWillAppear, viewWillDisappear),但它不起作用,当我回到这个 VC 时,标题保持绿色。应用程序中的主要颜色也是绿色:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    UIApplication.shared.statusBarStyle = .lightContent

    DispatchQueue.main.async {
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont(name: "Gotham-Medium", size: 20)!]
    }        
}


override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.shared.statusBarStyle = .default

    navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.green, NSAttributedStringKey.font: UIFont(name: "Gotham-Medium", size: 20)!]


}
4

3 回答 3

1
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    UIApplication.shared.statusBarStyle = .lightContent

    DispatchQueue.main.async {
       addTitleLabel()
    }        
}


override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.shared.statusBarStyle = .default
}

func addTitleLabel(){
   var titleLabel: UILabel = UILabel()
   titleLabel.textColor = .white
   titleLabel.textAlignment = .center
   titleLabel.text = "Home"

  self.navigationItem.titleView = titleLabel
}

以 viewWillAppear 的形式调用此方法。

于 2018-01-13T20:21:31.957 回答
1

由于导航栏如何在视图控制器之间共享以及系统如何更新它,这不会按照您的意愿工作。

但是,您可以做的是在导航栏的标题中添加您想要的字体和颜色的 UILabel。这种方法的优点是 UILabel 只适用于那个特定的视图控制器,所以你永远不需要重置它。

因此,在推送(第二个)视图控制器的 viewDidLoad 中放置以下代码:

let label = UILabel()
label.font = UIFont(name: "Menlo", size: 20)
label.textColor = .white
label.text = self.navigationItem.title
self.navigationItem.titleView = label

(请注意,您可以将文本设置为您想要的任何内容,但 self.navigationItem.title 保持简单)

您现在可以从 viewWillAppear 和 viewWillDisappear 方法中删除与导航栏相关的代码。

于 2018-01-13T22:20:56.977 回答
0

您必须在视图中添加导航标题颜色代码将出现在以前的视图控制器中。

于 2018-01-13T19:49:34.353 回答