Change Navigation Bar Attributes in Only One ViewController - ios

I have an application's navigation bar set in the AppDelegate.
In one of my ViewController's I'm trying to set the attributes differently - they work however when I get to other VC's the still persist.
What is the correct way to implement this?
AppDelegate.swift
UINavigationBar.appearance().titleTextAttributes = [
NSFontAttributeName: UIFont(name: "MuseoSans-500", size: 19)!,
NSForegroundColorAttributeName: UIColor.white
]
UINavigationBar.appearance().tintColor = UIColor.white
SharkViewController.swift
// MARK: - View Did Load
override func viewDidLoad() {
super.viewDidLoad()
UINavigationBar.appearance().titleTextAttributes = [
NSFontAttributeName: UIFont(name: "MuseoSans-500", size: 19)!,
NSForegroundColorAttributeName: UIColor.black
]
}

Do it in your viewWillAppear:
UINavigationBar.appearance().titleTextAttributes = [
NSFontAttributeName: UIFont(name: "MuseoSans-500", size: 19)!,
NSForegroundColorAttributeName: UIColor.black
]
And then when you leave the view, in your viewWillDisappear, you just reset the orgiginal value.

The best way is to change the appearance of the navigationItem of your viewController:
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.font: UIFont(name: "MuseoSans-500", size: 19)!]
navigationItem.standardAppearance = appearance
This will change only your viewController appearance and you can do it either in the viewDidLoad or viewWillAppear.

You also can use on viewDidLoad: or viewWillAppear the following code:
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "MuseoSans-500", size: 19)!,
NSForegroundColorAttributeName: UIColor.black]

Related

Navigation Bar title font problem on ios 13

I’m using Xcode 11.4 and iOS 13.4.
I have set navigation bar title custom font using UINavigatinBar.appearance()
And it works correctly but on iOS 13+ when i try to push to another VC and then comeback to the parent VC, the parent VC title font suddenly has been set to default font and after a second it changes back to the custom font.
Below is a gif of the problem:
nav bar font problem
iOS 13.+ has UINavigationBarAppearance approach to customize NavigationBar-Title & NavigationBar-BarButtonItems
Check this code, might help you
let titleFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.white ]
let barButtonFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 14)! ]
UINavigationBar.appearance().tintColor = UIColor.white // bar icons
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .red // If you want different nav background color other than white
appearance.titleTextAttributes = titleFontAttrs
appearance.largeTitleTextAttributes = titleFontAttrs // If your app supports largeNavBarTitle
UINavigationBar.appearance().isTranslucent = false
appearance.buttonAppearance.normal.titleTextAttributes = barButtonFontAttrs
appearance.buttonAppearance.highlighted.titleTextAttributes = barButtonFontAttrs
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
} else {
UINavigationBar.appearance().barTintColor = .red // bar background
UINavigationBar.appearance().titleTextAttributes = titleFontAttrs
UINavigationBar.appearance().isTranslucent = false
UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .highlighted)
}
Here you go, manage it in viewDidAppear:
let lblTitle = UILabel()
let titleAttribute: [NSAttributedString.Key: Any] = [.font: UIFont.boldSystemFont(ofSize: 21),
.foregroundColor: UIColor.black]
let attributeString = NSMutableAttributedString(string: "Navigation Title", attributes: titleAttribute)
lblTitle.attributedText = attributeString
lblTitle.sizeToFit()
navigationItem.titleView = lblTitle

Custom font on UINavigationBar not working

I have no idea why this is not working, I've tried it with many valid strings (cFont = "Courier" by the way but I've tried "Chalkduster" and a few others to test), the colors right above are working but the font is not. Any ideas?
Function is right below the didFinishLaunchingWithOptions in AppDelegate.
func setUpAppearance() {
//Work fine
window?.tintColor = Colors().navBackgroundColor
UINavigationBar.appearance().barTintColor = Colors().navBackgroundColor
UINavigationBar.appearance().tintColor = Colors().navTextColor
//Doesn't work
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: cFont, size: 24)!]
}
Thank you for the help
Got it! Didn't realize I had to add a separate instance for the bar button items as well as NSForeGroundColorAttributeName.
Updated function works fine...
func setUpAppearance() {
window?.tintColor = Colors().navBackgroundColor
UINavigationBar.appearance().barTintColor = Colors().navBackgroundColor
UINavigationBar.appearance().tintColor = Colors().navTextColor
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: cFont, size: 24)!, NSForegroundColorAttributeName: Colors().navTextColor]
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: cFont, size: 24)!, NSForegroundColorAttributeName: Colors().navTextColor], forState: .Normal)
}

How to change the font and text color of the title of UINavigationBar

I want to be change the font of the title to Avenir, and I want to make it white. Here is my code in the viewDidLoad method:
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Avenir", size: 20)!]
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
This code only changes the color of the title, not the font. Additionally, I tried implementing this in the App Delegate File, but that did not work.
Here is what my UINavigationBar looks like:
I want the title to have a font of Avenir as well. How do I achieve this?
It seems this is needed as well:
self.navigationController?.navigationBar.barStyle = UIBarStyle.Black // I then set the color using:
self.navigationController?.navigationBar.barTintColor = UIColor(red: 204/255, green: 47/255, blue: 40/255, alpha: 1.0) // a lovely red
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() // for titles, buttons, etc.
let navigationTitleFont = UIFont(name: "Avenir", size: 20)!
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: navigationTitleFont]
If you want the styles to be applied through out the whole app add these lines in the AppDelegate.m file.
NSShadow* shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(0.0f, 0.0f);
shadow.shadowColor = [UIColor blackColor];
[[UINavigationBar appearance] setTitleTextAttributes: #{
NSForegroundColorAttributeName: [UIColor whiteColor],
NSFontAttributeName: [UIFont fontWithName:#"Kelvetica Nobis" size:20.0f],
NSShadowAttributeName: shadow
}];
NSForegroundColorAttributeName sets the Font color.
NSFontAttributeName sets a custom font face to the title text.
NSShadowAttributeName applies a nice shadow effect to the text, you can remove this part if you want.
Also in advance, you can add these line to hide the text that comes up in back button in action bar when you navigate to another view within the navigation controller.
[[UIBarButtonItem appearance]
setBackButtonTitlePositionAdjustment:UIOffsetMake(-1000, -1000)
forBarMetrics:UIBarMetricsDefault];
Hope this help your question :)
In case someone needs this in Swift 4:
let navBarAppearance = UINavigationBar.appearance()
navBarAppearance.barTintColor = .red
navBarAppearance.tintColor = .white
navBarAppearance.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont(name: "Avenir", size: 20)!]
Your code is OK you just need to set all of titleTextAttributes in one line:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white , NSFontAttributeName: UIFont(name: "Avenir", size: 17)!]
In Swift 5:
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont(name: "Avenir", size: 20)!]
appearance.largeTitleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont(name: "Avenir", size: 35)!]
Code here are for Title and Large title. you can also remove custom font if you want system font.
I'm updating this for iOS 15. If you have a scroll view that pushes something behind the Nav Bar it WILL change the NavigationBar background if you do NOT set "navigationBar.scrollEdgeAppearance".
So here is a simple config to set it for ALL of your views in ViewDidLoad of your main ViewController.
// Set top Nav Bar behavior for ALL of app
let standardAppearance = UINavigationBarAppearance()
// Title font color
standardAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
// prevent Nav Bar color change on scroll view push behind NavBar
standardAppearance.configureWithOpaqueBackground()
standardAppearance.backgroundColor = UIColor.blue
self.navigationController?.navigationBar.standardAppearance = standardAppearance
self.navigationController?.navigationBar.scrollEdgeAppearance = standardAppearance
So the 'scrollEdgeAppearance' you can set title color, tint, NavBar color and the works. The standardAppearance is what shows at loading.
Took me a while to figure this one out for iOS15.
You're welcome! :-)
I would just create an outlet and do this:
#IBOutlet weak var navigationBar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Avenir", size: 30)!]
}

How to change UINavigationItem font?

I am trying to change font property for UINavigationItem.
I've tried using titleTextAttributes but somehow I am only able to change title font.
How can I change the font or UINavigationItem ? For example, the "More" text for the Back UIButton shown below:
I've tried using:
self.navigationController?.navigationBar.titleTextAttributes = [
NSForegroundColorAttributeName: UIColor.blackColor(),
NSFontAttributeName: UIFont(name: "Papyrus", size: 18)!
]
but it only changes what is shown on the picture, not the "More" Button's Font.
The reason your method wasn't working is because you were just using titleTextAttributes = ... when you have to use setTitleTextAttributes:forState: I use this function to customize the Nav Bar globally for my entire project:
func customizeNavBar(_ color: UIColor, titleFont: UIFont, buttonFont: UIFont) {
UINavigationBar.appearance().tintColor = color
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: color, NSFontAttributeName: titleFont]
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: color, NSFontAttributeName: buttonFont], forState: UIControlState.Normal)
}
For a single instance, call the same functions:
someBarButton.tintColor.setTitleTextAttributes([NSForegroundColorAttributeName: color, NSFontAttributeName: buttonFont], forState: UIControlState.Normal)
Swift 4
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15)!], for: .normal)
For iOS 13 and Swift 5.
For setting the bar button item text color & font:
UIBarButtonItem.appearance().setTitleTextAttributes([
.foregroundColor: UIColor.white,
.font: UIFont(name: GetFontNameBold(), size: 40)! ],
for: UIControl.State.normal)
For setting the title color & font just add in viewDidLoad() the following line:
UINavigationBar.appearance().titleTextAttributes = [
.foregroundColor: UIColor.white,
.font: UIFont(name: "Arial", size: 24)! ]
if you have outlet do this:
titleItem.titleLabel.font = UIFont(name: NAME, size: SIZE)

Cannot Set Navigation Bar Font in Swift on Latest Xcode Version

The following code was running fine before updating to Xcode 6.1 to stay up with iOS 8.1:
override func viewDidAppear(animated: Bool) {
self.navigationController?.navigationBar.topItem?.title = "Home"
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34), NSForegroundColorAttributeName: UIColor.whiteColor()]
}
The issue is specifically in NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)
and the error I'm getting here is:
! "Could not find an overload for 'init' that accepts the supplied arguments"
I found this original code on a different StackOverflow question and it was working as expected until this update (downloaded it yesterday). My font is indeed installed properly.
Should I be writing this code differently now, or is there an entirely new way in which I should set my Navigation Bar Font?
Thanks!
Whoops. I figured this out on my own:
I needed an exclamation point following my declaration of the NSFontAttributeName as it requires a type "NSString!". Perhaps it only required an "NSString" before, but I have no issues now.
Working line:
NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 24)!
Working full code:
override func viewDidAppear(animated: Bool) {
self.navigationController?.navigationBar.topItem?.title = "Home"
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
}
Seems like a silly question now. Hope this helps someone else!
You are using UIFont(name: intializer as it is defined as
init?(name fontName: String, size fontSize: CGFloat) -> UIFont
failable intializer read more from link.So it is returning optional.You need to unwrap it as it require AnyObject not optional.
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
Thanks Benji!!!
I changed it a bit and applied it to the appearance attribute of the navigation controller.
var navigationBarAppearance = UINavigationBar.appearance()
navigationBarAppearance.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "OpenSans", size: 16)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
It is better to declare your font as conditional , like this way :
if let font = UIFont (name: "AdobeArabic-BoldItalic", size: 20) {
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: font]
}
Doing this , makes sure that your font is already found , that I have installed new font , and when used it without conditional if , it issued an exception , of unwrapping an optional .
override func viewWillLayoutSubviews() {
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName : UIFont(name: "Roboto-Regular", size: 14)!]
}
Swift 4.2
Change Small Title when layout scrolled down
override func viewDidAppear(animated: Bool) {
self.navigationController?.navigationBar.topItem?.title = "Home"
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34), NSForegroundColorAttributeName: UIColor.whiteColor()]
}
Change Large Title
override func viewDidAppear(animated: Bool) {
self.navigationController?.navigationBar.topItem?.title = "Home"
self.navigationController?.navigationBar.largeTitleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34), NSForegroundColorAttributeName: UIColor.whiteColor()]
}

Resources