Changing navigation bar color in Swift
Navigation Bar:
navigationController?.navigationBar.barTintColor = UIColor.green
Replace greenColor with whatever UIColor you want, you can use an RGB too if you prefer.
Navigation Bar Text:
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.orange]
Replace orangeColor with whatever color you like.
Tab Bar:
tabBarController?.tabBar.barTintColor = UIColor.brown
Tab Bar Text:
tabBarController?.tabBar.tintColor = UIColor.yellow
On the last two, replace brownColor and yellowColor with the color of your choice.
Changing Navigation Bar Color in Xcode
You just need to click on the navbar and in attribute inspector, there is an option for Background, you can provide any color which you want to give. In my case, I selected a custom sky blue color.
How to dynamically change navigation bar color on ios15?
In your VC you can try adjusting the view.backgroundColor
UINavigationBar?.view.backgroundColor = MY_COLOR
swift: change the color of the navigation bar
Instead of barTintColor
, use backgroundColor
to change the navigationBar'r
color, i.e.
In FirstVC
,
class FirstVC: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationItem.largeTitleDisplayMode = .always
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.backgroundColor = .red //here...
}
}
In SecondVC
,
class SecondVC: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.backgroundColor = .clear //here...
}
}
Change current colour of NavigationBar in iOS15
the NavBar current-colour change based on user input while the app is running(after application(didFinisLaunching) has been called) is not working
You cannot change a navigation bar color while that navigation bar is showing by using the appearance
proxy. The appearance
proxy affects only future interface elements. You need to apply your UINavigationBarAppearance to the existing navigation bar directly:
self.navigationController?.navigationBar.standardAppearance = appearance
self.navigationController?.navigationBar.scrollEdgeAppearance = appearance
Changing colour of navigation bar
Use navigationBar.barTintColor
From Apple's Document: https://developer.apple.com/documentation/uikit/uinavigationbar/#1654191
You can specify a custom tint color for a navigation bar background using the barTintColor property. Setting this property overrides the default color inferred from the bar style. As with all UIView subclasses, you can control the color of the interactive elements within navigation bars, including button images and titles, using the tintColor property.
Could not change navigationBar.backgroundColor
, because it is hidden by a few views.
You can see the view hierarchy by a break point.
Why the NavigationBar background color not change?
Well, it has taken a little time to figure out how to fix this, and the solution is very simple.
Just set barTintColor
in navigationBar
to color you need.
private func changeToLightColor() {
self.navigationController?.navigationBar.barStyle = .default
//Set to white color
self.navigationController?.navigationBar.barTintColor = UIColor.white
let textAttribute = [NSAttributedString.Key.foregroundColor: UIColor.systemBlue]
self.navigationController?.navigationBar.titleTextAttributes = textAttribute
}
private func changeToDarkColor() {
self.navigationController?.navigationBar.barStyle = .black
//Set to black color
self.navigationController?.navigationBar.barTintColor = UIColor.black
let textAttribute = [NSAttributedString.Key.foregroundColor: UIColor.systemGreen]
self.navigationController?.navigationBar.titleTextAttributes = textAttribute
}
After I did it, the issue has gone
Swift & Navigation : Navigation Bar changes its background color when scroll the view
Paste this to AppDelegate and if you have tab bar also then paste tabbarappearance also it will work.
if #available(iOS 15, *) {
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithOpaqueBackground()
navigationBarAppearance.titleTextAttributes = [
NSAttributedString.Key.foregroundColor : UIColor.white
]
navigationBarAppearance.backgroundColor = UIColor.blue
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UINavigationBar.appearance().compactAppearance = navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
let tabBarApperance = UITabBarAppearance()
tabBarApperance.configureWithOpaqueBackground()
tabBarApperance.backgroundColor = UIColor.blue
UITabBar.appearance().scrollEdgeAppearance = tabBarApperance
UITabBar.appearance().standardAppearance = tabBarApperance
}
Related Topics
How to Scroll to the Exact End of the Uitableview
Does iOS Provide Built in Text to Speech Support or Any Class Like Nsspeechrecognizer
iOS 9 Searchbar Disappears from Table Header View When Uisearchcontroller Is Active
Cropping Center Square of Uiimage
Reload Tableview Section Without Scroll or Animation
Avaudiosession Setcategory Swift 4.2 iOS 12 - Play Sound on Silent
App Transport Security Does Not Work Any More with iOS 11 Xcode 9
iOS Floating Video Window Like Youtube App
Xcode 8, iOS 10 - "Starting Webfilter Logging for Process"
iOS Extensions with Multiple Targets
Uitableviewrowaction Title as Image Icon Instead of Text
Swift 3 How to Get Date for Tomorrow and Yesterday ( Take Care Special Case ) New Month or New Year
Can't Set Titleview in the Center of Navigation Bar Because Back Button