Change the font on the back button of a navigation controller under swift 4
For Swift 4, U can try this in AppDelegate.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red, NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 15)!], for: UIControlState.normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.green, NSAttributedStringKey.font : UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)! ], for: .highlighted)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.blue, NSAttributedStringKey.font : UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)! ], for: .focused)
return true
}
In ViewController.
override func viewWillAppear(_ animated: Bool) {
self.title = "List"
self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!]
// THE BELOW THREE LINES NOT WORKING.
//navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .normal)
//navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .highlighted)
//navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .focused)
}
Storyboard
Output
How can I change the font of the back button for my navigation bar?
Just tested your code and it seems the reason that line is returning nil
is actually because name: "FONTNAME"
returns nil. So if you set that name
attribute to a valid font name, the code should run without an error -- even if navigationController?.navigationItem.leftBarButtonItem
is explicitly set to nil.
But regardless, also as I've seen through testing, this line won't give you the result you apparently want. The leading navigationController
optional shouldn't be there since it accesses your UINavigationController
and not the current view. Simply use the UIViewController
's own navigationItem
property to access its leftBarButtonItem
directly, ex:
let backButton = UIBarButtonItem(title: "< Back", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack")
navigationItem.leftBarButtonItem = backButton
navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Chalkduster", size: 20)!], forState: UIControlState.Normal)
Edit: From the comment you posted under this answer, it seems as if you don't actually want to set the leftBarButtonItem
but the backBarButtonItem
because you don't want to implement a custom action beyond going back to the previous view controller.
So in that previous view controller (i.e. the view before you want to show your custom back button item), you can set your custom back button without an action like so:
let backButton = UIBarButtonItem(title: "< Back", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
backButton.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Chalkduster", size: 20)!], forState: UIControlState.Normal)
navigationItem.backBarButtonItem = backButton
How can I change the font and color of the Navigation Bar back button
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
}
How to change font size of Navigation controller back button
You can change it inside AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "SF Pro Display", size: 17)!], for: .normal)
return true
}
Where you can customize the font and size of the Navigation bar buttons so it changes in all View Controllers.
Change font of back navigation bar button
To change the appearance of the text in all UIBarButtonItems
appearing in all UINavigationBars
, do the following in application:didFinishLaunchingWithOptions:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:
@{UITextAttributeTextColor:[UIColor blackColor],
UITextAttributeTextShadowOffset:[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
UITextAttributeTextShadowColor:[UIColor whiteColor],
UITextAttributeFont:[UIFont boldSystemFontOfSize:12.0]
}
forState:UIControlStateNormal];
UPDATE: iOS7 friendly version
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor whiteColor];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor blackColor],
NSShadowAttributeName:shadow,
NSFontAttributeName:[UIFont boldSystemFontOfSize:12.0]
}
forState:UIControlStateNormal];
Swift:
NOTE: this changes ALL instances of UIBarButtonItem
, not just those contained within a UINavigationBar
UIBarButtonItem.appearance()
.setTitleTextAttributes([NSFontAttributeName : ExamplesDefaults.fontWithSize(22)],
forState: UIControlState.Normal)
Swift3:
UIBarButtonItem.appearance()
.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "FontName-Regular", size: 14.0)!],
for: .normal)
Change UINavigationBar back button text and font from AppDelegate using Swift
Swift 3.0,4.0
Simply you can achieve it with extension
of UINavigationItem
. According to many search there is no way to change left button text with app delegate.
extension UINavigationItem{
override open func awakeFromNib() {
super.awakeFromNib()
let backItem = UIBarButtonItem()
backItem.title = "Hello"
if let font = UIFont(name: "Copperplate-Light", size: 32){
backItem.setTitleTextAttributes([NSFontAttributeName:font], for: .normal)
}else{
print("Font Not available")
}
/*Changing color*/
backItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.green], for: .normal)
self.backBarButtonItem = backItem
}
}
Update:
You can change Back Button arrow colour from AppDelegate
on didFinishLaunchingWithOptions
,
/*It will change back arrow color only if you use backItem.setTitleTextAttributes, else it will change whole text color*/
UINavigationBar.appearance().tintColor = UIColor.orange
Swift change font and color of back button
If you want to set same color to bar buttons implicitly then in your AppDelegate
, in didfinishlaunchingwithoptions
, write:
UINavigationBar.appearance().tintColor = UIColor.white //your desired color here
Update :
Put this in AppDelegate
,
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal) // your textattributes here
Update 2 :
UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UINavigationBar.classForCoder()]).setTitleTextAttributes(["attribute" : "value"], forState: .Normal)
Hope this will help :)
Change font of back button on UINavigationController
Use this instead, default function available in iOS 5:
UIBarButtonItem *backbutton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:nil action:nil];
[backbutton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor],UITextAttributeTextColor,
[UIFont fontWithName:TEXTFONT size:16.0f],UITextAttributeFont,nil]
forState:UIControlStateNormal];
Change the default font of Back button in UINavigationController
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:
@{UITextAttributeTextColor:[UIColor blackColor],
UITextAttributeTextShadowOffset:[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
UITextAttributeTextShadowColor:[UIColor whiteColor],
UITextAttributeFont: myCustomFont
}
forState:UIControlStateNormal];
Related Topics
Changing Text of Uibutton Programmatically Swift
What Does Addchildviewcontroller Actually Do
Creating a Uiimage from a Uicolor to Use as a Background Image for Uibutton
In iOS, How to Create a Button That Is Always on Top of All Other View Controllers
How to Throttle Search (Based on Typing Speed) in iOS Uisearchbar
Multiple Uilabels Inside a Self Sizing Uitableviewcell
Document Directory Path of Xcode Device Simulator
How to Get the Font Name from an Otf or Ttf File
In iOS 12, When Does the Uicollectionview Layout Cells, Use Autolayout in Nib
How to Simultaneously Satisfy Constraints - No Constraints in Place
Differencebetween Pan and Swipe in iOS
Nsdate Beginning of Day and End of Day
Swift - How to Remove a Decimal from a Float If the Decimal Is Equal to 0
How to Set Adaptive Multiline Uilabel Text
Dealing with iPad Mini Screen Size
Uitableviewcell Show White Background and Cannot Be Modified on iOS7