I've adjusted the default tint of my navigation bar to be white and it appropriately adjusts the color of each of my navigation bar elements:
However, when I push to a new view controller and try to set the title property, the tint is no longer applied:
I know that I can supply a label or something similar to the titleView attribute of my view controller that would do the trick, but that's a lot of work (relatively speaking) and in my mind the text should just default to the navigation bar's tint color. Am I missing something else? Or is this standard behavior that requires a custom titleView to override?
The tint property does not affect the color of the title. To set the title color (along with other attributes like font) globally, you can set the titleTextAttributes property of the UINavigationBar appearance to suit your needs. Just place this code in your AppDelegate or somewhere else appropriate that gets called on launch:
Swift 3:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
Swift 2
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
No you work correctly. But you should to set color for second view. You can use this code to solve your problem.
In second view write this code to set color and font for your navigation title.
navigationController!.navigationBar.titleTextAttributes = ([NSFontAttributeName: UIFont(name: "Helvetica", size: 25)!,NSForegroundColorAttributeName: UIColor.white])
Related
My application has a setting screen that allows the user to change app color including navigation bar and tab bar tint colors and also allow him to change app font and font size for all texts in the app like in Telegram application. the problem is when user select a color for a list of specific colors I need something like reloads or refreshes my application to make the change affect the tab bar and navbar of the application
I found this question but it changes the color one time when the app launched but I want to allow the user to customize the application color and font
Changing navigation bar color in Swift
After a long search, I found the answer to fix the problem of the change navigation bar and tab bar color, tint color, and font in run time when the user chooses a specific color.
to change the color of the navigation bar:
use this to change the color of navbar for the current screen
navigationController?.navigationBar.barTintColor = Color
and that for changing the color for navbar in the entire app
UINavigationBar.appearance().tintColor = Color
UINavigationBar.appearance().barTintColor = Color
and this for changing navbar font for the current screen
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: AppFont().large]
and this for the entire app
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: AppFont().large]
and this for change tab bar tint color
self.tabBarController?.tabBar.tintColor = Color.theme.value
and this to change tab bar items font
let selectedAttrs = [NSAttributedString.Key.font: Font, NSAttributedString.Key.foregroundColor: Color]
if let items = self.tabBarController?.tabBar.items {
for item in items {
item.setTitleTextAttributes(selectedAttrs, for: .selected)
}
}
https://gph.is/g/ZOR7bAP
What is the best way of changing colors for Title and Back button of UINavigationBar from Storyboard?
What is the best way of changing colors for Title and Back button of UINavigationBar from Storyboard?
The color of the title is an attribute of the navigation bar, so select the navigation controller's navigation bar:
and then look at the Attributes Inspector, where you can set the title color:
The color of the Back button is controlled by the tint color. You can set the global tint color in the File Inspector for the storyboard:
These settings should work fine if you want to set the title and tint color once for the whole app, but if you want different colors for different view controllers, then one way or another you're going to have to write some code. If that's something you need to do often, and you want to be able to set the colors in IB, you could consider writing your own UIViewController subclass from which all your view controllers are derived. Give that common controller class inspectable attributes for the colors you want to set, and of course add code that sets them appropriately. You'll probably want to use UIAppearance for that.
Bear in mind, though, that the reason that these colors aren't already attributes of UIViewController is that the colors are supposed to help give your app a consistent appearance; changing your app's color scheme from one scene won't be a favor to your users.
you can do it by code
navigationController?.navigationBar.backgroundColor = UIColor.red
navigationController?.navigationBar.isTranslucent = false
let button1 = UIBarButtonItem(image: UIImage(named: "search.png"), style: .plain, target: self, action: #selector(barBut)) // action:#selector(Class.MethodName) for swift 3
self.navigationItem.rightBarButtonItem = button1
navigationController?.navigationBar.tintColor = .white
I am trying to change the color of the text "Welcome" in the Navigation Bar.
As you see now it is blue and I want to change it to white as the rest of the text in the Navigation Bar.
Would you give me any ideas? I tried to add some code and it is working, because the clock was black and now is white, like it should be, but the "Welcome" is still staying in blue.
Put this code in either your app delegate in the didFinishLaunchingWithOptions method, or in your navigationController's custom class
let customColor = UIColor.redColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:customColor]
UINavigationBar.appearance().tintColor = customColor
I have set a color of my Navigation bar in Appdelegate so my app uses same color for all the navigation appearing in different screens. But Now for one or two controllers I want to change the navigation color. I used this code in viewDidLoad of specific controller but It isn't working
self.navigationController?.navigationBar.backgroundColor = UIColor.whiteColor()
I have tried this code in viewWillAppear function too but still it doesn't work
The property you are looking for is not backgroundColor, but rather barTintColor.
self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
Please use tint color
[nav.navigationBar setBarTintColor:[UIColor blueColor]];
I am having an issue in my app with a particular back button changing font colour for no apparent reason. I have been through all of my code and I can't see any reason for it, but when launching my app and segueing to the problem view controller, the text colour seems to be set to the default blue colour.
If I tap to go to a different tab and then go back to this view controller, then the font returns to the correct colour (white) and this is despite not having any code in viewWillAppear/Disappear viewDidAppear/Disappear.
The code I am using to set the navigation bar text colour is:
override func awakeFromNib() {
var attributes = [NSForegroundColorAttributeName: UIColor.whiteColor(),NSFontAttributeName: UIFont(name: "Avenir", size: 24)!]
self.navigationController?.navigationBar.titleTextAttributes = attributes
}
And this is in the view controller that is segued from when going to the view controller with the back button. I have tried adding this code into viewWillAppear() but even this doesn't make a difference.
Does anyone know why, on launch, my app doesn't set the back button text colour and requires a tab switch for it to update?
Please Try to write same code in view will apear method.Than it should persist style to back button
I think this code will work
self.navigationController?.navigationBar.barTintColor = UIColor.someColor()
//This sets the background colour of the navigation bar
self.navigationController?.navigationBar.tintColor = UIColor.someColor()
// This sets the colour for navigation and bar button items
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "someFont", size: 20)!, NSForegroundColorAttributeName: UIColor.blackColor()]
//This sets the attributed text for the title of the navigation bar
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont(name: "someFont", size: 17)!], forState: .Normal)
//And this sets the attributed text for the bar button item(s) on the navigation bar (So that includes the back button)
Call it in the viewWillAppear
I hope this works :)