Unable to change navigation bar color - ios

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.

Related

"PrefersLargeTitles" preventing background color change in all navigation bars

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.

How to add view on top of navigation bar

How to add view above navigation bar?
I have a custom navigation controller and I want to present a view above nav bar (like on the screen), so it should be visible on other ViewControllers
Would be great if the solution will be on storyboard.
Tried to add on UIWindow did't help.
Swift 4.2, Xcode 10+
Okay, from what I can tell (via your comment reply, though it still isn't 100% clear), the best solution to your question would be to make the navigation bar transparent, such that you can see any navigationController-presented view controllers underneath it. For this, I'd suggest the following extension to UIViewController:
extension UIViewController {
func setupTransparentNavigationBarWithBlackText() {
setupTransparentNavigationBar()
//Status bar text and back(item) tint to black
self.navigationController?.navigationBar.barStyle = .default
self.navigationController?.navigationBar.tintColor = .black
}
func setupTransparentNavigationBarWithWhiteText() {
setupTransparentNavigationBar()
//Status bar text and back(item) tint to white
self.navigationController?.navigationBar.barStyle = .blackTranslucent
self.navigationController?.navigationBar.tintColor = .white
}
func setupTransparentNavigationBar() {
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.backgroundColor = .clear
self.navigationController?.navigationBar.isTranslucent = true
}
}
Using either of the first two methods in viewWillAppear of your UIViewController subclasses will let you make the navigation bar completely transparent with the statusBar text + wifi/battery indicators black or white as desired. From this, you can then display anything under the navigation bar by pinning your constraints to view.bounds.topAnchor. E.g. for a transparent navigation controller with white statusBar text:
class YourViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
setupTransparentNavigationBarWithWhiteText()
}
}

Tab Bar Color Slightly Changing with View BackgroundColor

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

why UINavigationBar color change after return form aAnother viewController with diferente color

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.

setting tabbar translucent not working

I want to make my tabbar half transparent by setting the translucent value to true. However it is not doing the trick. I have a TabBarVC assigned to the tab bar with the following code. The tab bar remains solid
class TabBarVC: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// color of background -> This works
self.tabBar.barTintColor = UIColor.purpleColor()
// color when selected -> This works
self.tabBar.tintColor = UIColor.redColor()
// This does not work
self.tabBar.translucent = true
}
I also tried to do something like
UIColor(red: 246.0/255, green: 246.0/255, blue: 246.0/255, alpha: 0.5)
But it does not seem to work. I did a bit of search on Google but everyones issue seems to differ from mine. Could anyone help me out here?
Thanks,
What you're doing is most likely working to adjust the transparency of the tab bar. However, you need to set the corresponding view controllers to be "Under Bottom Bar" in the IB.

Resources