How can I change the color of UINavigationController item?
I use Embed In UINavigationController => UIViewController and I have by default "< Back". I want to delete this "Back" text and change the color of item to white.
I did try the next:
override func viewDidLoad() {
super.viewDidLoad()
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
}
inside of the UIViewController inside of which this item appears, but it doesn't help me
Use self.navigationController?.navigationBar.barTintColor to change the color
self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
You can change navigation bar color with following code.
self.navigationController.navigationBar.barTintColor = UIColor.greenColor()
And change Navigation bar text.
self.navigationController.navigationBar.titleTextAttributes = [UITextAttributeTextColor: UIColor.whiteColor()]
Edited
To change Text color of bar button
UIBarButtonItem.appearance().setTitleTextAttributes([UITextAttributeTextColor: UIColor.whiteColor()], forState: UIControlState.Normal)
Related
I use this to set NavigationBarColor before it run:
UINavigationBar.appearance().barTintColor = Color.NavigationBackground
But in the program,I want to change the NavigationBarColor, So I use this again
UINavigationBar.appearance().barTintColor = Color.Black
But nothing happen, It still white(Color.Background)
Color is a struct that I defined.
How to change the color correctly?
I want to achieve like this:Trying to reload view controller to update the current theme
if you want each screen to have different color add below line with color of your choice in view will appear and it will change color for each screen.
Swift 4.2:
//To change Navigation Bar Background Color
UINavigationBar.appearance().barTintColor = UIColor.blue
//To change Back button title & icon color
UINavigationBar.appearance().tintColor = UIColor.white
//To change Navigation Bar Title Color
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
add it in view will appear and then you can see it changing.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.barTintColor = .orange
}
if you want to change the color of navigation bar color
navigationController?.navigationBar.barTintColor = UIColor.black
Use the appearance API, and barTintColor color.
UINavigationBar.appearance().barTintColor = UIColor.red
In your ViewController's viewWillAppear(_:) simply set navigationBar's barTintColor as your required color, i.e.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.barTintColor = .red
}
Use this code to set UINavigationBar color initially in your Appdelegate’s didFinishLaunchingWithOptions.
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().backgroundColor = Color.Background
UINavigationBar.appearance().barTintColor = Color.Background
UINavigationBar.appearance().tintColor = UIColor.white
And when you want to change color of UINavigationBar within app, just use these lines of code. Let’s say change color is your button action.
#IBAction func changeThemeColor(_ sender: UIButton) {
self.navigationController?.navigationBar.backgroundColor = Color.Black
self.navigationController?.navigationBar.barTintColor = Color.Black
}
For some reason I can't figure out why I'm not able to change the tintColor of my navigation bar buttons in my app and I don't know how to figure out why. Can you recommend how I can debug this issue?
I am adding this to the viewDidLoad of my view controller:
UINavigationBar.appearance().tintColor = .green
However, nothing changes and the back button and Aa bar button stay the default grey color.
The navigation + status bars are translucent and sit on top of the background color of the viewcontroller through out the app.
I am able to change the barbutton to .green in a test app where I do the exact same (add it to ViewDidLoad of a vc, make sure that the barbutton images assets are set to Template so they'll take on the color I set). But for whatever reason, that same thing won't change in my app prototype.
Are there are any common reasons for this that I might not know of?
Added: the bar button was added via storyboard only.
And here is the code attempt to change its color via the viewDidLoad of 1 viewcontroller:
Class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UINavigationBar.appearance().tintColor = .green
}
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
}
}
If you want to use the global appearance() proxy, then you can't do it in your view controller.
If you want to change the tintColor in viewDidLoad of your view controller then you have to set it on your controller's navigationController instance directly:
navigationController?.navigationBar.tintColor = .green
To use the appearance() proxy, and set a global tintColor for navigation bars, you'll have to do it before your navigation controller is initialized.
For example, it will work if you do it in your app delegate's application(_:didFinishLaunchingWithOptions:):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UINavigationBar.appearance().tintColor = .green
return true
}
See the UIAppearance documentation for details of why this is:
https://developer.apple.com/documentation/uikit/uiappearance
"iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back."
Navigation Bar color
To change the background color of the navigation bar, use the below code
navigationController?.navigationBar.barTintColor = .green
Change .green with your desired color.
Navigation Bar Text Color
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.green]
Change .green with your desired color.
UINavigationBar.appearance().barTintColor = UIColor(hex:"YourColorcode")
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName: UIFont(name:"AvenirNext-Medium", size:19.0)]
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName: "YourFontName"], for: UIControlState.normal)
I have 2 view controllers embedded in a navigation bar controller. Plus each view controller has a table view that covers the entire view.
The issue I am experiencing is that my back button (this is the left bar button item) in the 2nd view controller is not visible though I can tap on the top left corner of the screen and it takes me back to the previous view controller. Thus, the button is present just not visible.
I do have this code in my App Delegate because I need to change the appearance of the header title of the Navigation Bar. Is this overriding my Nav Bar in some way?
-AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//change navigation bar appearance
//--background color
UINavigationBar.appearance().barTintColor = UIColor.white
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
//--title color
let fontColor = UIColor(red: 80/255.0, green: 80/255.0, blue: 80/255.0, alpha: 1.0)
let font = UIFont(name: "Verdana-Bold", size: 20)!
let attributes: [String: AnyObject] = [
NSFontAttributeName: font,
NSForegroundColorAttributeName: fontColor
]
UINavigationBar.appearance().titleTextAttributes = attributes
return true
}
I did verify in the Attribute Inspector in my Navigation Controller Scene that the property 'Shows Navigation Bar' is checked.
I even tried to add a refresh button programmatically to the 1rst view controller like so and I can see the print statement in the console meaning that when I press the top left corner the console prints "Refresh Button Pressed" but again the button is not visible even though it functions.
-FirstViewController.swift
// MARK: - Target Action functions
#IBAction func refreshAction(_ sender: UIButton) {
print("Refresh Button Pressed")
//let _ = self.navigationController?.popViewController(animated: true)
}
func setNavigationBarAttributes() {
//set title for navigation bar (the appearance was changed in App Delegate)
self.navigationItem.title = "NYC Schools"
//refresh button
let refreshButton = UIButton(type: .custom)
refreshButton.setImage(UIImage(named: "back_button.png"), for: .normal) // Image can be downloaded from here below link
refreshButton.setTitle("Refresh", for: .normal)
refreshButton.setTitleColor(refreshButton.tintColor, for: .normal) // You can change the TitleColor
refreshButton.addTarget(self, action: #selector(self.refreshAction(_:)), for: .touchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: refreshButton)
}
I think the issue with your invisible, working back button in your 2nd view controller is the fact that you have set the navigation bar barTintColor to white, and also the tintColor to white. This gives you a white back button on a white navigation bar - making it invisible!
Try it again with these settings in your AppDelegate:
UINavigationBar.appearance().barTintColor = UIColor.white
UINavigationBar.appearance().tintColor = UIColor.blue
As for your refresh button on the 1st view controller - I think there's two possibilities for why you can't see it:
"back_button.png" is a white or transparent image, which is not visible against the white navigation bar.
the "back_button.png" image you specified does not exist and you're creating a button with no image at all, which (I think) would render it invisible.
I have a navigation bar created by the nav controller for my view, these are loaded via a container side menu.
When i click an item, it loads the nav controller and view, but the nav bar background drops out showing a blank background colour on the status bar.
Any idea how I can diagnose this issue? I have included some view debugger screenshots to best illustrate
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().barTintColor = UIColor().appThemeColour()
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
UIApplication.shared.statusBarStyle = .lightContent
Well you are missing one thing here and thats setBackgroundImage of UINavigationBar.
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().barTintColor = UIColor().appThemeColour()
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
UIApplication.shared.statusBarStyle = .lightContent
*Note this is the solution for Sidemenu Library named "https://github.com/jonkykong/SideMenu/"
Change status bar end alpha property to 0
Please find code below.
#objcMembers
open class SideMenuPresentationStyle: InitializableClass {
/// Background color behind the views and status bar color
open var backgroundColor: UIColor = .white`
I am having view controllers
And I am showing like this
Let assume 1,2,3 are the view controllers.
I navigate like this
1->2->3
When I come back
3->2->1
then from 1 the navigation bar button item cancel of UIImagePicker doesn't show. If I don't go with this sequence (1->2->3
) It shows perfectly.
Please let me know what could be the issue.
I am using opaque navigation bar.
And the navigation bar tint and bar tint color I am updating like this in the appdelegate class
func setNavigationAppearance(tintColor : UIColor, barTintColor : UIColor?) {
let navigationBarAppearace = appDelegateObj.navigationController!.navigationBar
navigationBarAppearace.tintColor = tintColor
navigationBarAppearace.barTintColor = tintColor
navigationBarAppearace.translucent = false
//navigationBarAppearace.
//Settign the Custome Font and TextColor
if let font = UIFont(name: FontCustom.Regular, size: 17) {
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: font ,NSForegroundColorAttributeName : tintColor]
}
}
And adding the background image to the navigation bar like this
if let myImage = UIImage(named: AppImagesName.PatternRed) {
UINavigationBar.appearance().setBackgroundImage(myImage, forBarMetrics: .Default)
}