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
}
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)
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.
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 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
How to change back button text in Navigation item to picture in xcode
Add below code in viewDidLoad
of viewController where you want to change back button title:
let backButton = UIBarButtonItem()
backButton.title = ""
self.navigationController?.navigationBar.topItem?.backBarButtonItem = backButton
How do I change the title of the back button on a Navigation Bar
This should be placed in the method that calls the ViewController titled "NewTitle".
Right before the push or popViewController statement.
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:@"NewTitle"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
[newBackButton release];
Related Topics
View at the Bottom in a Uiscrollview, with Autolayout
Can't Prevent 'Touchmove' from Scrolling Window on iOS
Create and Store PDF Document Programmatically Using Swift for iOS
Convert JSON String to JSON Object in Swift 4
iOS 13 - How to Login in In-App Purchase Sandbox Account
iOS in App Purchase: Test a Real Purchase Without Submitting to Apple
Showing a Uipickerview with Uiactionsheet in iOS8 Not Working
Add Nsurlconnection Loading Process on Uiprogressview
iOS App Won't Run on Device Any More Under iOS 12: Unrecoverable Ct Signature Issue
Wkwebview Didn't Finish Loading, When Didfinishnavigation Is Called - Bug in Wkwebview
Nsarray with Nspredicate Using Not In
How to Convert an iPhone Osstatus Code to Something Useful
Slservicetypefacebook Setinitialtext Is Not Working
What Do JSONserialization Options Do and How Do They Change JSONresult
Will iOS Wake Up the Terminated App If It's Registered with Location for Uibackgroundmodes
Xcode 4.3: Codesign Operation Failed (Check That the Identity You Selected Is Valid)