问题
I am facing the navigation title color not change when I pop controller. Please find the below code.
ProfilescreenVC.swift
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.tintColor = UIColor.white
self.navigationController?.navigationBar.barTintColor = UIColor.primaryGreen
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
self.navigationController?.navigationBar.titleTextAttributes = textAttributes
}
EditprofileVC.swift
override func viewDidLoad() {
super.viewDidLoad()
self.style()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func style() {
navigationController?.navigationBar.tintColor = UIColor.black
navigationController?.navigationBar.barTintColor = UIColor.white
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.black]
navigationController?.navigationBar.titleTextAttributes = textAttributes
carbonTabSwipeNavigation.setIndicatorColor(UIColor.black)
carbonTabSwipeNavigation.setTabBarHeight(50.0)
carbonTabSwipeNavigation.setNormalColor(UIColor.hexString("9A9CA1"), font: UIFont.systemFont(ofSize: 15))
carbonTabSwipeNavigation.setSelectedColor(UIColor.black, font: UIFont.systemFont(ofSize: 15))
self.navigationController?.view.setNeedsLayout()
self.navigationController?.view.layoutIfNeeded()
}
override func viewDidDisappear(_ animated: Bool) {
navigationController?.navigationBar.tintColor = UIColor.white
navigationController?.navigationBar.barTintColor = UIColor.white
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
navigationController?.navigationBar.titleTextAttributes = textAttributes
super.viewDidDisappear(animated)
}
I have a facing issue with the color changing of the title please check the following video for better understanding.
回答1:
This seems to be some awkward issue in which navigation bar's title attributes are only applied by going forwards.
After trying everything I could I resolved to the following workaround where I provided my own titleView in navigationItem.
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
titleView.backgroundColor = UIColor.clear
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
titleLabel.textAlignment = .center
titleLabel.text = "Profile"
titleLabel.textColor = .white
titleView.addSubview(titleLabel)
self.navigationItem.titleView = titleView
self.navigationItem.title = "Profile"
Note that you also need to provide your title in navigationItem.title so that your back button is set properly when you navigate forward.
回答2:
ProfilescreenVC.swift
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
self.navigationController?.navigationBar.tintColor = UIColor.white
self.navigationController?.navigationBar.barTintColor = UIColor.primaryGreen
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
self.navigationController?.navigationBar.titleTextAttributes = textAttributes
}
and in editProfileVC
override func viewWillAppear(_ animated: Bool)
{
navigationController?.navigationBar.tintColor = UIColor.black
navigationController?.navigationBar.barTintColor = UIColor.white
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.black]
navigationController?.navigationBar.titleTextAttributes = textAttributes
}
No need to add ViewDidDisapper in your code
来源:https://stackoverflow.com/questions/50094290/uinavigation-title-color-change-issue