I use this code to change my navigation bar appearance which I put it in AppDelegate:
UINavigationBar.appearance().barTintColor = UIColor(hex: "E1354A")
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
Also I use UISearchController which I add programmatically. But when I push it the color of navigation bar and search controller are changed in black. I don't understand why it happens and how I can prevent it?
[][1
I've found a solution. I put in viewDidLoad:
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = UIColor(hex: "E1354A")
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance
let searchField = searchController.searchBar.searchTextField
searchField.backgroundColor = .systemBackground
Related
I am trying to remove a border from UINavigationBar while making it completely white so that it blends with the background.
Here is the code which I have:
nav.navigationBar.standardAppearance.shadowColor = UIColor.white //also tried UIColor.clear
nav.navigationBar.barTintColor = UIColor.white
nav.navigationBar.backgroundColor = UIColor.white
nav.navigationBar.barStyle = .default
nav.navigationBar.isTranslucent = false
The end result is depicted in the image below. As you can see, my navigation bar is not completely white:
If I remove the line where I set shadowColor I do get a white background, but with a line at the bottom.
I would appreciate if someone could help me to get the result which I want.
Check out this code:
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .white
appearance.shadowColor = .none
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
UINavigationBar.appearance().tintColor = .black
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
Is there a way to do the transition cleaner, without these white lines?
In rootView I've:
navigationController?.navigationBar.prefersLargeTitles = true
and in Detailview:
navigationItem.largeTitleDisplayMode = .never
In AppDelegate:
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .BlackNav
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
Thanks for your answers!
EDIT:
Solved: the lines disappear if you don't define UINavigationBar.appearance().compactAppearance:
UINavigationBar.appearance().standardAppearance = appearance
//UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
I am having some problems coloring the navigationBar. I have tried using different methods from some different tutorials but nothing seems to work the way that I want it to work.
I am using the following code in my AppDelegate:
let navAppeareance = UINavigationBarAppearance()
navAppeareance.configureWithOpaqueBackground()
navAppeareance.backgroundColor = .systemRed
//Setup buttons
let buttonDone = UIBarButtonItemAppearance(style: .done)
buttonDone.normal.titleTextAttributes = [.foregroundColor: UIColor.white]
navAppeareance.doneButtonAppearance = buttonDone
let buttonPlain = UIBarButtonItemAppearance(style: .plain)
buttonPlain.normal.titleTextAttributes = [.foregroundColor: UIColor.white]
navAppeareance.buttonAppearance = buttonPlain
//Set appearances
UINavigationBar.appearance().standardAppearance = navAppeareance
UINavigationBar.appearance().scrollEdgeAppearance = navAppeareance
UINavigationBar.appearance().backgroundColor = .systemRed
The above code yields the following result:
I want the statusbar to be the same color as the navigationBar, but it takes the color of the backgroundColor of the view.
use this in AppDelegate
let navBarAppearnce = UINavigationBar.appearance()
navBarAppearnce.barTintColor = UIColor.red // the color you want
as many iOS devs out there i'm facing some issues with iOS 13 update.
One of these was the different management of the status bar style
On iOS 12 i used to set the navigation bar style like this
self.navigationController?.navigationBar.barStyle = .black
which affects the status bar style, setting it to white (because the navigation bar style is black);
but it doesn't seem to work on iOS 13, i guess it has something to deal with
UINavigationBarAppearance()
class
I configured my navigation bar for each ViewController like this:
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.accessibilityTextualContext = .sourceCode
navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.backgroundColor = .brownCircles
navBarAppearance.shadowImage = nil // remove navigationBar Bottom border
navBarAppearance.shadowColor = nil // remove navigationBar Bottom border
self.navigationController?.navigationBar.standardAppearance = navBarAppearance
self.navigationController?.navigationBar.compactAppearance = navBarAppearance
self.navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
} else {
self.navigationController?.navigationBar.barTintColor = .blue
self.navigationItem.title = NSLocalizedString(kTitle, comment: kTitle.capitalized)
}
self.navigationController?.navigationBar.barStyle = .black
so far so good, but
self.navigationController?.navigationBar.barStyle = .black
works just on iOS 12, nothing happens on iOS 13 the status bar still looks black instead of white
Did anyone face this issue?
Finally i figured out!
the magic code to set a light status bar text is:
self.navigationController?.navigationBar.overrideUserInterfaceStyle = .dark
of course if you want to change to dark text i have to set it to .light.
Some things to notice:
This code:
if #available(iOS 13.0, *) {
overrideUserInterfaceStyle = .dark
}
although it should set the entire view and subviews to dark, doesn't seem to affect the status bar.
You can also use:
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
but of course is deprecated so i'd recommend other ways
You still need:
self.navigationController?.navigationBar.barStyle = .black,
but put it AFTER the UINavigationBarAppearance() settings and after the self.navigationController?.navigationBar.overrideUserInterfaceStyle = .dark.
Final code will look like this:
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.accessibilityTextualContext = .sourceCode
navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.backgroundColor = .brownCircles
navBarAppearance.shadowImage = nil // remove navigationBar Bottom border
navBarAppearance.shadowColor = nil // remove navigationBar Bottom border
self.navigationController?.navigationBar.standardAppearance = navBarAppearance
self.navigationController?.navigationBar.compactAppearance = navBarAppearance
self.navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
self.navigationController?.navigationBar.overrideUserInterfaceStyle = .dark
} else {
self.navigationController?.navigationBar.barTintColor = .blue
self.navigationItem.title = NSLocalizedString(kTitle, comment: kTitle.capitalized)
}
self.navigationController?.navigationBar.barStyle = .black
Hope it helps! ;)
I want to change the navigation bar background colour, title font and colour programmatically in iOS 11 and swift 4 from AppDelegate. I know how to do it using Xcode but didn't find up-to-date solution for doing it programmatically.
Here are the steps for doing it for only specific ViewControllers.
I have created a BaseViewController file which is the parent for all of my ViewControllers. And the following code as been added to the viewDidLoad() of the BaseViewController.
For changing the Navigation bar's background color
self.navigationController?.navigationBar.barTintColor = UIColor.white
For changing Navigation bar's title and Bar button colors
self.navigationController?.navigationBar.tintColor = UIColor.black
For changing font
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.red, NSAttributedStringKey.font : UIFont.sourceSansPro(ofSize: 18.0), NSAttributedStringKey.kern:1.5]
You can use the following code to change background colour and Title font.
func setupNavigationBarAppearance() {
UINavigationBar.appearance().tintColor = .black
UINavigationBar.appearance().shadowImage = UIImage.imageFromColor(.black, width: 1.0, height: 1.0)?.resizableImage(withCapInsets: .zero, resizingMode: .tile)
UINavigationBar.appearance().isTranslucent = false
let font:UIFont = UIFont(name: "ProximaNova-Bold", size: 18.0)!
let navbarTitleAtt = [
NSAttributedStringKey.font:font,
NSAttributedStringKey.foregroundColor: UIColor.white
]
UINavigationBar.appearance().titleTextAttributes = navbarTitleAtt
}
And call this func in didFinishLaunchingWithOptions as setupNavigationBarAppearance(). I am using this same code, and it is working fine.
Just use UINavigationBar.appearance()
For example:
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.white]
or
UINavigationBar.appearance().barTintColor = .blue
For AppDelegate:
Put following code in didFinishLaunchingWithOptions in AppDelegate:
UINavigationBar.appearance().barTintColor = UIColor(red: 46.0/255.0, green: 14.0/255.0, blue: 74.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
UINavigationBar.appearance().isTranslucent = false
On iOS 15 it is now
navigationController.navigationBar.backgroundColor = .white