zachjs zachjs - 1 year ago 111
iOS Question

Change font of back navigation bar button

I want to be able to set the font of my apps navigation bar back button without doing anything too crazy and without losing any other design characteristics of the button (i.e. I want to keep the arrow).

Right now I use this in

to set the font of the normal bar button items.

for (NSObject *view in self.navigationController.navigationBar.subviews) {
if ([view isKindOfClass:[UIButton class]]) {
[((UIButton*)view).titleLabel setFont:[UIFont
fontWithName:@"Gill Sans"

However this makes no change on the back button, regardless of which
this code is applied to (root, current, etc.).

Answer Source

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]

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]
 @{NSForegroundColorAttributeName:[UIColor blackColor],
   NSFontAttributeName:[UIFont boldSystemFontOfSize:12.0]


NOTE: this changes ALL instances of UIBarButtonItem, not just those contained within a UINavigationBar

               .setTitleTextAttributes([NSFontAttributeName : ExamplesDefaults.fontWithSize(22)], 
                                       forState: UIControlState.Normal)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download