Author Topic: How do I change the font of navigation buttons?  (Read 4101 times)


  • Administrator
  • *****
  • Posts: 719
    • View Profile
How do I change the font of navigation buttons?
« on: November 11, 2013, 10:55:04 AM »
Assuming that you want the same font for all bar button items in your app, you can add a call to UIBarButtonItem's appearanceWhenContainedIn: method in the AppDelegate file's application:didFinishLaunchingWithOptions: method.

Unfortunately, the appearanceWhenContainedIn: method is not available in Swift. To make it available, you need to do the following:

1. Create an extension of the UIBarButtonItem class:

Code: [Select]
// UIBarButtonItem+Swift.h
@interface UIBarButtonItem (UIBarButtonItemAppearance_Swift)

// Fix appearanceWhenContainedIn: not available in Swift
+ (instancetype)appearanceWhenContainedInSwift:(Class<UIAppearanceContainer>)containerClass;

Code: [Select]
// UIBarButtonItem+Swift.m
@implementation UIBarButtonItem (UIBarButtonItemAppearance_Swift)
+ (instancetype)appearanceWhenContainedInSwift:(Class<UIAppearanceContainer>)containerClass {
    return [self appearanceWhenContainedIn:containerClass, nil];

2. Add the following import statement to your project's bridging header file:

Code: [Select]
#import "UIBarButtonItem+Swift.h"
3. Add the following code to the AppDelegate.didFinishLaunchingWithOptions: method:

Code: [Select]
            [ NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 18)!],
            forState: UIControlState.Normal)

Here's how you do it in Objective-C:

Code: [Select]
// Change the font for all bar button items
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:
@{ NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Light" size:18.0] }

All the best!
Kevin McNeish
Author of "Learn to Code in Swift":
« Last Edit: July 17, 2015, 02:46:01 PM by kjmcneish »