ios - UINavigationBar 标题颜色

标签 ios uinavigationbar swift4 viewwillappear viewwilldisappear

为什么当我回到主 UIViewController 时,navigationBar 标题没有将其颜色更改为白色?这是简单的代码(viewWillAppearviewWillDisappear),但它不起作用,当我回到这个 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)!]


}

最佳答案

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 调用此方法。

关于ios - UINavigationBar 标题颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48243361/

相关文章:

ios - 尝试在 UIViewController 被关闭后再次呈现它

iPhone 子类化 View Controller 和 IBOutlet UITableView

ios - 防止启动图像覆盖状态栏上的时间

ios - 如何使用 swift3 将已排序的结构放入另一个结构/字典中?

swift - 如何将导航栏后退按钮图像设置为默认符号?

ios - Swift 4 使用 CodableFirebase 和自定义类处理来自 Firestore 的 nil 值

ios - NSInteger 乘法 : Invalid operands to binary expression

ios - 在标签栏 Controller 上使用导航栏

ios - 如何覆盖 "default"导航栏?

ios - 在 UICollectionView 中引用 UIImageView/UILabel