I have three controller. let's say Controller A, B and C in sequence.
navigation title color in controller A is red.
blue in controller B and green in controller C.
When i push these viewcontroller navigation title color change perfectly. But when coming back to B from C or A from B navigation tile becomes green and blue respectively(sould be blue and red).
What i did:
I have embedded navigation controller from interface builder in storyboard. To change the title color i used following code in viewWillAppear of every view controller. I placed all my code in main queue. also checked without not placing them in async block and results are same.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
Dispatch.main.async{
self.navigationItem.title = "viewcontroller2"
let textAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]
self.navigationController?.navigationBar.titleTextAttributes = textAttributes
}
}
Note that: This problem occur when i use navigation back button to go back but while swipe to back, title color works as it should be. iOS version : 11.4.1. In iOS ver 10.3.3 this issue is not occurring.
Please use like,
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]
self.navigationController?.navigationBar.layoutIfNeeded()
Related
I have a tableViewController as my root view controller.
I've been trying to change the color of the status bar to match something like this:
However, when I set:
navigationController?.navigationBar.prefersLargeTitles = true
And apply:
override func viewDidAppear(_ animated: Bool) {
navigationController?.navigationBar.barTintColor = UIColor.blue
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.blue]
}
The background color doesn't change at all.
Only when I begin SCROLLING and the navigation bar collapses, do I get a color.
Has anyone else ran into this issue before? I built another viewController to test out "PrefersLargeTitles" WITHOUT a tableview scroll feature. And there was no background color either.
My problem is exactly as explained in this question ViewController title color will change change color when back button clicked
I've Navigation Controller -> View Controller (#1) -> Segue to another View Controller (#2)
View Controller #2 has a different title color.
When I click on Back button in View Controller #2 to go back to Home View (#1) it's title color is not changed to what it should be but it remain the title colour of view controller #2.
I've set the correct title color in attribute inspector. I'm also setting title color exclusively in Home View Controller's viewWillAppear but it's colour is still not changed.
I'm wondering what else I need to do? Is there
I've already added code to set title color to white in Home View Controller's viewWillAppear
override func viewWillAppear(_ animated: Bool){
super.viewWillAppear(animated)
print("HomeViewController: viewWillAppear()")
// Mark: - Set navigation bar title color
setTitleColorWhite(vc: self)
activityIndicator.startAnimating()
fetchDetails()
}
And this is setTitleColorWhite
// Mark: - Set navigation bar title color
func setTitleColorWhite(vc: UIViewController){
let attrs = [
NSAttributedStringKey.foregroundColor: UIColor(red:1.00, green:1.00, blue:1.00, alpha:1.0),
NSAttributedStringKey.font: UIFont(name: "SFProText-Medium", size: 17.0)!
]
vc.navigationController?.navigationBar.titleTextAttributes = attrs
}
Update the color in HomeViewController's viewDidAppear instead of viewWillAppear
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setTitleColorWhite(vc: self)
}
In my app, the primary theme has blue navigation bars with white bar button items and titles.
I set the values for colors like this in the App Delegate.
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().barTintColor = AppColors.blue
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
But in some view controllers, I'm required to invert these styles (white navigation bars with blue buttons and titles).
So in those view controllers, I simply set the new values in the viewWillAppear method.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.tintColor = AppColors.blue
navigationController?.navigationBar.barTintColor = .white
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: AppColors.blue]
}
From one of these view controllers, I have to show a UIDocumentPickerViewController to pick a document file.
let documentPickerViewController = UIDocumentPickerViewController(documentTypes: [String(kUTTypePDF)], in: .import)
documentPickerViewController.delegate = self
present(documentPickerViewController, animated: true)
The user can preview it as well. I use the QuickLook framework to do that.
The problem is these view controllers, UIDocumentPickerViewController and QLPreviewController don't conform to the new navigation bar color values I set in the viewWillAppear method.
Master view controller has the blue navigation bar but the detail view's navigation bar is all white. Including the bar button items. So the buttons are not visible. It seems the values I set in the viewWillAppear method has no effect here. I commented out the appearance values from the App Delegate and the buttons show up in the default colors.
Same with the QLPreviewController view controller.
I tried having the presenting view controller conform to UINavigationControllerDelegate but that didn't work.
I also tried getting a reference to the UIDocumentPickerViewController's navigation controller like this documentPickerViewController.navigationController but it returns nil as well.
How do I apply colors to the UIDocumentPickerViewController without changing the appearance values?
I uploaded a demo project here.
Is there anyway to keep the tab bar color exactly the same regardless of the color of the view in the view controllers? The bottom picture with dashboard selected is darker than the top one because the view.backGroundcolor = .lightGray in the view controller.
I tried setting the view.bottomAnchor equal to the view.safeAreaLayoutGuide.bottomAnchor, but even then if the view is set as light gray, the tab bar will be slightly darker than the view controllers that have a white background.
I also set the self.tabBar.barTintColor = .white
and self.tabBar.alpha = 1.0
It's not just a perception thing either as I checked the exact color in hexcode.
Relevant lines of code :
final class TabBarViewController: UITabBarController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBar.barTintColor = .white
self.tabBar.alpha = 1.0
}
}
Should this code go in the init instead that sets up the corresponding view controllers?
The color you are setting for the tab bar is only a tint. The only way to get absolute control over the color is to make a resizable UIImage of the desired color and set the tab bar's backgroundImage property.
https://developer.apple.com/documentation/uikit/uitabbar/1623469-backgroundimage
i have my base View controller with it´s embbed navigation controller so i set ist collor , when segue are executed the new view has it owns navigationbar color and it changes to it but after return to the back view this view takes the color from the previuos one.
i´m setting the color of the navigationbar like this
override func viewDidLoad() {
self.navigationController?.navigationBar.barTintColor = appDelegate.verde
}
its a defined color in the Appdelegate to be green color in the
in the next one i change in the color of the navigationbar the same way as above.
You change the color of the UINavigationBar and it remains changed unless you explicitly change it to something else. UINavigationBar does not depend on viewControllers - it's above them.
In order to have different color for each viewController when navigating between them back and forth, change UINavigationBar's color in viewWillAppear(_ animated: Bool).
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigationController?.navigationBar.barTintColor = appDelegate.verde
}
Instead of viewDidLoad you may put this code in viewWillAppear. This will be invoked each time the view is about to be presented so it will override any color changes for other views.