Can't change UINavigationBar Custom Font Color - swift

I am using Storyboard and I want to change the UINavigationBar title color to UIColor.whiteColor() on one of my ViewControllers. Xcode is recognizing the font, but I can't change the color.
I Tried:
changing it with both Storyboard and with code below, but it does not seem to work. I also tried using different fonts and both .otf and .ttf thinking it might be caused by an error with the fonts. So far I can only get a black color with any custom font.
var nav = self.navigationController!.navigationBar
self.navigationItem.title = "HOME"
nav.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
nav.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Impact", size: 30)!]

I found that you need to set the attributes at the same time like this:
nav.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont(name: "Impact", size: 30)!]

You can also set the navigation bar's tintColor property to your desired color.

Related

How can I change the font and color of the Navigation Bar back button

I am trying to change the font and color of the Navigation Bar button.
Ideally, I'd like to change it to black and use a custom font that I already have set up. Is there any way to modify the existing button?
I tried this in my view's init for the font
UINavigationBar.appearance().titleTextAttributes = [.font : UIFont(name: "Georgia-Bold", size: 20)!]
But I think this only changes the title of an inline Nav bar
It's a UIBarButtonItem so set this property.
init() {
UIBarButtonItem.appearance().tintColor = .red
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10)], for: .normal)
}
Or you can use this also,
init() {
let standard = UINavigationBarAppearance()
let button = UIBarButtonItemAppearance(style: .plain)
button.normal.titleTextAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10), NSAttributedString.Key.foregroundColor: UIColor.red]
standard.buttonAppearance = button
}
You can do the method above or:
Go to your Assets -> Set a colour for Accent, although this will affect other components in your app i.e. DisclosureGroup button etc.

SwiftUI - how do customise navigation bar on one screen only?

At WWDC, one talk gave an example of how the ‘Reminders’ app customises the Navigation Bar on different screens. They shared how they did this:
let appearance = navigationBar.standardAppearance.copy()
navigationItem.standardAppearance = appearance
How would the actual implementation work? I’m not quite sure where that code slots in, in order to customise one navigation bar.
Any help would be super appreciated!
In viewWillAppear set this value
UINavigationBar.appearance().titleTextAttributes = [
NSFontAttributeName: UIFont(name: "MuseoSans-500", size: 19)!,
NSForegroundColorAttributeName: UIColor.black
]
and on viewWillDisappear reset all the values.

Swift Navigation bar background color white does not work

Im having a difficult time trying to change the navigationBar.barTintColor to white. Strangely, all other colors work, but not white!
self.navigationController!.navigationBar.barTintColor = UIColor.whiteColor()
Above line doesnt work just for white.
Even tried with a background image. Still the same. Any other color works but not white!! White is always replaced by light grey...
Please advice where I am going wrong...
Thanks.
Try this Code:
In your viewDidLoad:
title = "Some Title"
UIApplication.shared.statusBarStyle = .default
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
//Line under NavigationBar
let barView = UIView(frame: CGRect(x:0, y:(UINavigationController().navigationBar.frame.height + UIApplication.shared.statusBarFrame.height), width:view.frame.width, height:0.6))
barView.backgroundColor=UIColor.red // set any colour you want..
navigationController?.navigationBar.addSubview(barView)
//Title Colour
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.red]
Note:
Above code works on any background colour....
If you want the navigationBar to be green....set your view background colour to green...

Change UIBarButtonItem from UISearchBar

I'd like to change the text font and color of the Cancel button inside the UISearchBar in iOS 8. I've tried the solutions for iOS 6 and 7 already and they don't seem to work.
This was not the solution I was looking for, but it worked.
let cancelButtonAttributes: NSDictionary = [NSFontAttributeName: FONT_REGULAR_16!, NSForegroundColorAttributeName: COLOR_BLUE]
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, forState: UIControlState.Normal)
What you are looking for is:
//The tintColor below will change the colour of the cancel button
searchBar.tintColor = UIColor.blueColor()
But these are also useful While we are on the subject:
searchBar.placeholder = "Placeholder"
//The barTintColor changes the colour of the flashing text indicator
searchBar.barTintColor = UIColor.redColor()
//This sets the cancel button to show up
searchBar.setShowsCancelButton(true, animated: true)
//This makes the searchBar become active
searchBar.becomeFirstResponder()

Custom font title for UIButton

Is there a way to have custom fonts to the title of a button.
The UIButton class exposes a titleLabel property, upon which you can change things like the font, the text color, etc.
Set the font of the button's titleLabel:
myButton.titleLabel.font = [UIFont fontWithName:#"Courier" size:22.0];
You can always render text in a UIImage and then configure the button to use that with setImage:forState:. This allows you to do custom fonts, labels with icons, images, etc.
In case you want to list all available fonts and then select your font, run this Swift code:
UIFont.familyNames().forEach {
print($0)
UIFont.fontNamesForFamilyName($0).forEach {
print(" \($0)")
}
}
It prints all font names for their font families. Then assign the same font name as printed.
button.titleLabel.font = UIFont(name: "OpenSans", size: 20.0)