I use this to set NavigationBarColor before it run:
UINavigationBar.appearance().barTintColor = Color.NavigationBackground
But in the program,I want to change the NavigationBarColor, So I use this again
UINavigationBar.appearance().barTintColor = Color.Black
But nothing happen, It still white(Color.Background)
Color is a struct that I defined.
How to change the color correctly?
I want to achieve like this:Trying to reload view controller to update the current theme
if you want each screen to have different color add below line with color of your choice in view will appear and it will change color for each screen.
Swift 4.2:
//To change Navigation Bar Background Color
UINavigationBar.appearance().barTintColor = UIColor.blue
//To change Back button title & icon color
UINavigationBar.appearance().tintColor = UIColor.white
//To change Navigation Bar Title Color
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
add it in view will appear and then you can see it changing.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.barTintColor = .orange
}
if you want to change the color of navigation bar color
navigationController?.navigationBar.barTintColor = UIColor.black
Use the appearance API, and barTintColor color.
UINavigationBar.appearance().barTintColor = UIColor.red
In your ViewController's viewWillAppear(_:) simply set navigationBar's barTintColor as your required color, i.e.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.barTintColor = .red
}
Use this code to set UINavigationBar color initially in your Appdelegate’s didFinishLaunchingWithOptions.
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().backgroundColor = Color.Background
UINavigationBar.appearance().barTintColor = Color.Background
UINavigationBar.appearance().tintColor = UIColor.white
And when you want to change color of UINavigationBar within app, just use these lines of code. Let’s say change color is your button action.
#IBAction func changeThemeColor(_ sender: UIButton) {
self.navigationController?.navigationBar.backgroundColor = Color.Black
self.navigationController?.navigationBar.barTintColor = Color.Black
}
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.
For some reason I can't figure out why I'm not able to change the tintColor of my navigation bar buttons in my app and I don't know how to figure out why. Can you recommend how I can debug this issue?
I am adding this to the viewDidLoad of my view controller:
UINavigationBar.appearance().tintColor = .green
However, nothing changes and the back button and Aa bar button stay the default grey color.
The navigation + status bars are translucent and sit on top of the background color of the viewcontroller through out the app.
I am able to change the barbutton to .green in a test app where I do the exact same (add it to ViewDidLoad of a vc, make sure that the barbutton images assets are set to Template so they'll take on the color I set). But for whatever reason, that same thing won't change in my app prototype.
Are there are any common reasons for this that I might not know of?
Added: the bar button was added via storyboard only.
And here is the code attempt to change its color via the viewDidLoad of 1 viewcontroller:
Class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UINavigationBar.appearance().tintColor = .green
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
}
}
If you want to use the global appearance() proxy, then you can't do it in your view controller.
If you want to change the tintColor in viewDidLoad of your view controller then you have to set it on your controller's navigationController instance directly:
navigationController?.navigationBar.tintColor = .green
To use the appearance() proxy, and set a global tintColor for navigation bars, you'll have to do it before your navigation controller is initialized.
For example, it will work if you do it in your app delegate's application(_:didFinishLaunchingWithOptions:):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UINavigationBar.appearance().tintColor = .green
return true
}
See the UIAppearance documentation for details of why this is:
https://developer.apple.com/documentation/uikit/uiappearance
"iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back."
Navigation Bar color
To change the background color of the navigation bar, use the below code
navigationController?.navigationBar.barTintColor = .green
Change .green with your desired color.
Navigation Bar Text Color
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.green]
Change .green with your desired color.
UINavigationBar.appearance().barTintColor = UIColor(hex:"YourColorcode")
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName: UIFont(name:"AvenirNext-Medium", size:19.0)]
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName: "YourFontName"], for: UIControlState.normal)
I need to make the navigation bar in some view controllers transparent (but with the bar buttons visible).
I wrote the following extension for that.
extension UINavigationBar {
func setTransparent(_ flag: Bool) {
if flag == true {
setBackgroundImage(UIImage(), for: .default)
shadowImage = UIImage()
backgroundColor = .clear
isTranslucent = true
} else {
setBackgroundImage(nil, for: .default)
}
}
}
The default styles for my navigation bars are as follows.
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().barTintColor = UIColor(red: 45/255, green: 93/255, blue: 131/255, alpha: 1)
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
All this works fine. But there's a problem if I have to turn off the transparent effect.
Say in the first view controller I don't need the navigation bar to be transparent.
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.setTransparent(false)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.isTranslucent = false
}
}
I push to the second view controller from here. In here, the navigation bar is transparent.
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.setTransparent(true)
}
}
Now when I pop back to the previous view controller, I have to explicitly set the isTranslucent property to false. I do that in the viewWillAppear as you can see in the first code snippet.
But the problem is, the navigation bar is black for a second when it happens.
I want this to be smooth. How do I avoid this?
Demo project uploaded here.
I tried the solution described here to a similar question. But it doesn't completely fix my issue. The black bar is gone but the navigation bar doesn't appear for a second just like before as you can see here.
Black navigation bar you see is actually a navigation controller view background color. Try add this code in first view controller viewDidLoad method
navigationController?.view.backgroundColor = navigationController?.navigationBar.barTintColor
Setting the navigation controller view background color did fix the black issue for me, but I was still having the "delay" problem when popping a view controller.
I fixed it by changing the theme of my NavigationController on the willMove method of the view controller being popped. Something like this:
override func willMove(toParent parent: UIViewController?) {
super.willMove(toParent: parent)
guard parent == nil,
let navController = self.navigationController else {
return
}
navController.navigationBar.isTranslucent = false
navController.view.backgroundColor = backgroundColor
navController.navigationBar.barTintColor = barColor
navController.navigationBar.tintColor = tintColor
}
Before Push I use
if let navigator = self.navigationController {
navigator.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigator.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.black]
navigator.pushViewController(viewController, animated: true)
}
this make the glitch disappear working fine...
note in my case background color was white
I am trying to change the color of my navigation bar in a view which contains a UISearchController. I have previously set the color of my navigation bars for the whole app in my appDelegate, but i want this views nav bar to have a different color. The issue is that i dont know which function to place the code such that it will override the appDelegate code. For example, viewDidLoad and viewWilAppear do not change the color when the view first loads, only after i enter and cancel the searchController. Which function should i place the following?
UINavigationBar.appearance().backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
There are two ways to do this. You can either modify the appearance proxy that you have set up in the AppDelegate for the whole app, or you can modify the individual navigationbar for the particular screen.
When you dismiss the view - you need to reset the barTintColor in the viewWillDisappear.
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()
}
Instead of changing the navigation bars color directly, you should change the navigation controller's bar color in viewWillAppear and change it back when you dismiss the view.
How can I change the color of UINavigationController item?
I use Embed In UINavigationController => UIViewController and I have by default "< Back". I want to delete this "Back" text and change the color of item to white.
I did try the next:
override func viewDidLoad() {
super.viewDidLoad()
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
}
inside of the UIViewController inside of which this item appears, but it doesn't help me
Use self.navigationController?.navigationBar.barTintColor to change the color
self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
You can change navigation bar color with following code.
self.navigationController.navigationBar.barTintColor = UIColor.greenColor()
And change Navigation bar text.
self.navigationController.navigationBar.titleTextAttributes = [UITextAttributeTextColor: UIColor.whiteColor()]
Edited
To change Text color of bar button
UIBarButtonItem.appearance().setTitleTextAttributes([UITextAttributeTextColor: UIColor.whiteColor()], forState: UIControlState.Normal)