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

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.

Related

How can I change my custom cell background when pressing a button?

I have a CollectionView and a Button inside of an UIView. The purpose of the button is change the theme of the app, and I can change all colors, except for the backgroundColor of a nameContainerView on my TatodexCell (a custom cell).
I can't figure out how to access that property, and then, change the color I want.
THE FOLLOWING CODE IS LOCATED ON MY TatodexController FILE
My button property is:
let buttonChangeTheme: UIButton? = {
let button = UIButton()
button.setTitle("Change to blue theme", for: .normal)
button.addTarget(self, action: #selector(themeButtonClicked), for: .touchUpInside)
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.layer.borderWidth = 3
button.layer.borderColor = Colors.mainBlack?.cgColor
button.backgroundColor = Colors.darkBlue
button.tintColor = Colors.mainWhite
return button
}()
My button-func is this one:
#objc func themeButtonClicked() {
clickCheck = !clickCheck
if clickCheck {
navigationController?.navigationBar.barTintColor = Colors.lightBlue
collectionViewPokemon?.backgroundColor = Colors.darkBlue
buttonChangeTheme?.backgroundColor = Colors.darkRed
buttonChangeTheme?.setTitle("Return to red theme", for: .normal)
}
else {
navigationController?.navigationBar.barTintColor = Colors.lightRed
collectionViewPokemon?.backgroundColor = Colors.darkRed
buttonChangeTheme?.backgroundColor = Colors.darkBlue
buttonChangeTheme?.setTitle("Change to blue theme", for: .normal)
}
}
THE FOLLOWING CODE IS LOCATED ON MY TatodexCell FILE
The nameContainerView I wanna modify it's bg color is:
lazy var nameContainerView: UIView = {
let nameView = UIView()
nameView.backgroundColor = Colors.lightRed
nameView.addSubview(nameLabel)
nameLabel.center(inView: nameView)
return nameView
}()
My goal is to change nameView.backgroundColor = Colors.lightRed to nameView.backgroundColor = Colors.lightBlue, but I can't access that property.
I would really appreciate any help or advice. Perhaps the solution is hiding in plain sight, but I've tried many ways and none of them worked. Let me know if another chunk of code is needed to be shown.
If you implement the tintColorDidChange() method in your custom collection view cell, and in that method, you set the cell's background color to the tint color, then when you change the owning UICollectionView's tint color, the cells's tintColorDidChange() methods will fire and you'll see the change.

UIActivityViewController: access the navigation items to change color of presented view

I am changing a project colors and this old orange color not changed on UIActivityViewController button. I want to change button color orange to white.
This look is when user press the share button and select "Add to Notes" button. How can I access these buttons and change colors?
First problem solved. This code is working on buttons:
UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .white
When I click to "New Note" this look is came. How can I change the title color on this view?
I want to change this title color black to white.
This is my sharing code and I was trying every bar tint color code.
func shareLink(_ link: String) {
let activityViewController = UIActivityViewController(activityItems: [link], applicationActivities: nil)
activityViewController.navigationController?.navigationBar.barTintColor = .white
activityViewController.navigationController?.navigationBar.tintColor = .white
activityViewController.tabBarController?.navigationItem.backBarButtonItem?.tintColor = .white
activityViewController.tabBarController?.navigationController?.navigationBar.tintColor = .white
self.present(activityViewController, animated: true)
}
Also I tried adding some code on AppDelegate in didFinishLaunchingWithOptions function
UINavigationBar.appearance().tintColor = .white
UIApplication.shared.keyWindow?.tintColor = .white
Ok. These buttons are not accessible. So I solved the problem as follows:
It's for all UINavigationBar buttons:
UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .white
It's for all UINavigationBar titles:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]

Autoshrink on custom nav bar title

I have a custom nav bar view so I can add multiple bar buttons to each side. Thus, I want to autoshrink the nav bar title rather than truncate it. I can't figure out how to do so with the following code I'm using.
navBar = UINavigationBar(frame: CGRectMake(0, 20, UIScreen.mainScreen().bounds.size.width, 44))
navBar.barTintColor = UIColor.blackColor() // Sets bar to black
navBar.translucent = false
self.view.addSubview(navBar)
navItem = UINavigationItem(title: "A Pretty Long String")
navBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 25)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
you can call this function
override func viewDidLayoutSubviews() {
println(self.view.frame.size)
println(self.view.navBar.frame.size)
}
and in that function change a variable which you use for the font size in the NSFontAttributeName function

Can't change UINavigationBar Custom Font Color

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.

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()