How to set navigation bar to solid color [duplicate] - swift

This question already has answers here:
Change the color of iOS Navigation Bar
(10 answers)
Changing navigation bar color in Swift
(36 answers)
Closed 4 years ago.
When I change the background color of the navigation bar, it is opaque like the following.
UINavigationBar.appearance().backgroundColor = .black
Then if I set translucent to false, I don't see any color like the following
UINavigationBar.appearance().backgroundColor = .black
UINavigationBar.appearance().isTranslucent = true
Any idea on how to make a solid background color?

Set it's barTintColor
Example:
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().barTintColor = .red

You should set the barTintColor instead of the backgroundColor:
The tint color to apply to the navigation bar background.
https://developer.apple.com/documentation/uikit/uinavigationbar/1624931-bartintcolor
UINavigationBar.appearance().barTintColor = .black

Related

Swift Navigation Bar not change userInterfaceStyle on scoll

I have a problem when I try to change interface style (dark or light). If I set dark mode I get the navigation bar remains light when I scroll. If I set light mode the navigation bar becomes black. How can I solve this?
I use this code to change userInterfaceStyle:
window?.overrideUserInterfaceStyle = .dark
I found a solution by myself.
For everyone that has this problem the solution is:
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .systemBackground
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = UINavigationBar.appearance().standardAppearance
put this code inside TabBarController

How to change programatically top bar background swift 4 on iOS 13

I want to change the status and navigation bar from colored to transparent and from transparent to colored.
How can I do this?
enter image description here
navigationController?.navigationBar.barTintColor = .green // Set Background Color
navigationController?.navigationBar.barTintColor = .none // remove Background Color

Erase borders between navigation bar and searchBar swift 4

I am setting both the navigation bar and the search bar to a custom UIColor (which I call categoryColor in my code). When I do that, I still see an upper grayish line between nav bar and search bar. I have already set the searchBar border color to be the same as the others, but that gray line still exists. Does anyone know how to get rid of it?
Here is my code:
override func viewWillAppear(_ animated: Bool) {
//defining the color that will be used for all the items
let categoryColor = UIColor(hexString: selectCategory?.categoryColorHex ?? UIColor.randomFlat.hexValue())
//changing navigation bar tint color
navigationController?.navigationBar.barTintColor = categoryColor
//changing searchbar tint color
searchBar.barTintColor = categoryColor
//change searchBar border's color
searchBar.layer.borderColor = categoryColor?.cgColor
searchBar.layer.borderWidth = 3
//changing title that appears at the top, after list is loaded
title = selectCategory?.listName ?? "Todoey"
}
Here is a picture of what I see when I run the simulation:
The better way for implementing search bar with nav controller would be to use searchController inside a navigationController so the searchController will have the same background as navigationController. Here’s a great tutorial about that: https://m.youtube.com/watch?v=h7caxpp3Xps
Edit:
Also if you already implemented search capabilities you can do that with searchController too. Just set navigationController.searchConroller.searchBar.delegate for class that’s responsible for handling delegate methods

Swift Navigation bar background color white does not work

Im having a difficult time trying to change the navigationBar.barTintColor to white. Strangely, all other colors work, but not white!
self.navigationController!.navigationBar.barTintColor = UIColor.whiteColor()
Above line doesnt work just for white.
Even tried with a background image. Still the same. Any other color works but not white!! White is always replaced by light grey...
Please advice where I am going wrong...
Thanks.
Try this Code:
In your viewDidLoad:
title = "Some Title"
UIApplication.shared.statusBarStyle = .default
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
//Line under NavigationBar
let barView = UIView(frame: CGRect(x:0, y:(UINavigationController().navigationBar.frame.height + UIApplication.shared.statusBarFrame.height), width:view.frame.width, height:0.6))
barView.backgroundColor=UIColor.red // set any colour you want..
navigationController?.navigationBar.addSubview(barView)
//Title Colour
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.red]
Note:
Above code works on any background colour....
If you want the navigationBar to be green....set your view background colour to green...

Remove UINavigationBar border [duplicate]

This question already has answers here:
How to hide UINavigationBar 1px bottom line
(49 answers)
Closed 7 years ago.
There is a thin gray border in the bottom of UINavigatioBar:
I can't remove it!
I've tried:
self.navigationController?.navigationBar.layer.borderColor =
UIColor.orangeColor().CGColor
self.navigationController?.navigationBar.layer.borderWidth = 0
With no chance.
It should work
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
Add this line for background color (Change rgb value of your choice)
self.navigationController?.navigationBar.barTintColor = UIColor(rgba: "#000000")