My app is using red navigation (2) bar with white buttons and texts. When I use system contact picker (3) the status bar is red. When I use documents picker (1) UIDocumentPickerViewController then navigation bar is white. How I can change color of navigation bar or text?
When I use code bellow, it works but it change my navigation bar too.
UINavigationBar.appearance().tintColor = .red
thanks for help
code:
func open() {
UINavigationBar.appearance().barTintColor = .green
let documentsController = UIDocumentPickerViewController(documentTypes: makeDocumentTypesList(), in: .import)
documentsController.delegate = self
viewControllerProvider.getViewController().present(documentsController, animated: true, completion: nil)
}
You can just reset the appearance for UIDocumentPickerViewController only by putting this code somewhere in your application:didFinishLaunchingWithOptions: function and the bar buttons will return to their original blue or you can set any other color of your choice. The bar color, on the other hand is not customizable.
if #available(iOS 11.0, *) {
UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentBrowserViewController.self]).tintColor = nil
}
Use setTitleTextAttributes
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.white], for: .normal)
see my answer here
This is the only solution I found:
UINavigationBar.appearance().tintColor = ... some color
_viewController?.present(documentPicker, animated: true)
To my knowledge it is not possible to set the bar color only the tint color (text color). To keep the tint color for the rest of your project I reset it in the viewWillAppear of the underlying view controller.
as you ask "change color of navigation bar or text?"
i haven't a solution for change navigation bar it always return nil
self.present(documentPicker, animated: true,completion: {
if documentPicker.navigationController?.navigationBar != nil{
documentPicker.navigationController?.navigationBar.barTintColor = .red
}
})
but if you agree to change only text
this works for me
self.present(documentPicker, animated: true,completion: {
documentPicker.view.tintColor = .red
})
i understand that it couldn't be the optimal but none of solutions i try work for me
Related
Since updating to Xcode 11.4 I'm running into the issue that I can't figure out how to change the color of the back arrow in the navigation bar.
Before I was able to simply use:
navigationBar.barTintColor = DisplayUtils.sweetGreenColor()
However, 11.4 has forced me to use the standardAppearance functionality to set the color of my navbar and everything. Which is no big deal, this works for setting the barTint and title colors:
navigationBar.standardAppearance.backgroundColor = DisplayUtils.sweetGreenColor()
let buttonAppearance = UIBarButtonItemAppearance()
buttonAppearance.normal.titleTextAttributes = [.foregroundColor : DisplayUtils.whiteColor()]
navigationBar.standardAppearance.buttonAppearance = buttonAppearance
navigationBar.standardAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: DisplayUtils.whiteColor(), NSAttributedString.Key.font: UIFont(name: "Roboto-Bold", size: 18) as Any]
I just don't see a property that replaced barTintColor on the UIBarButtonItemAppearance object. It lets you change the color of the text, change the image itself, but no color property.
I found a solution that works in my instance. It works because I'm creating my viewControllers in code such as:
if let stbrd = self.storyboard {
if let vc: ViewController = stbrd.instantiateViewController(withIdentifier: "VC") as? ViewController {
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil) // remove back button text
self.navigationItem.backBarButtonItem?.tintColor = DisplayUtils.sweetGreenColor()
self.navigationController?.pushViewController(vc, animated: true)
}
}
It's the line:
self.navigationItem.backBarButtonItem?.tintColor = DisplayUtils.sweetGreenColor()
That properly sets the color of the back arrow on the newly presented view controller.
I still don't see how to simply set that color on the view controller itself like before this change. So, if anyone can offer up that knowledge it would be great.
On the storyboard, for your Navigation Controller change the "Bar Tint" to its "Default" value, then on your code you can change it as you normally would.
By programmatically simply first change navigation bar color to default then you can update what you are already doing.
if #available(iOS 13.0, *) {
self.navigationController?.navigationBar.standardAppearance.configureWithDefaultBackground()
}
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 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()
}
}
I want to clear color of navigation bar. In my ViewController there is a background image on that, when i remove color of navigation barTintColor, navigationController.view.background and navigation background image then simulator shows me :-
I have been trying alots of codes but there is no solution found.
I want navigation Bar like that:-
with clear navigation bar color.
Is there any solution, let me know?
Thanks!
You can make the navigation bar transparent in viewWillAppear and remove transparency in viewWillDisappear as follows
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
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
self.navigationController?.navigationBar.shadowImage = nil
self.navigationController?.navigationBar.isTranslucent = false
}
The background image and the back button will be visible
Better you must avoid the navigation bar. Hide the navigation bar in the navigation controller and user custom view in your view controller to avoid this issue.
Swift 5:: Calling below in AppDelegate's didFinishLaunchingWithOptions function does the trick (This will be applied to your all navigationBars though, don't forget to switch your view controllers)
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithTransparentBackground()
navigationController?.navigationBar.standardAppearance = navBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
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