1

I have a couple of navigation items (Back Button, Save Button) on my NavigationBar. I tried looking for relevant answers, I find either an outdated answer or just none.

I tried setTitleTextAttributes on the navigationItem.leftBarButtonItem but did not work.

I managed to change the font of the NavigationBar title but that's it. Using self.navigationBar.titleTextAttribues, same method done with the nav items but not working.

EDIT: Found a solution, but I have to add this on EVERY view controller's viewWillAppear

let barBtn = UIBarButtonItem(title: "Test", style: UIBarButtonItemStyle.Bordered, target: self, action: "backButtonPressed")
barBtn.setTitleTextAttributes([NSFontAttributeName: UIFont(name: FONT_HERE, size: 12)!], forState: UIControlState.Normal)
self.navigationItem.setHidesBackButton(true, animated: false)
self.navigationItem.setLeftBarButtonItem(barBtn, animated: false)

Then adding the backButtonPressed function where I call a popViewControllerAnimated.

What do you think, guys? Is there a simpler solution?

Renz Tan
  • 245
  • 3
  • 10

1 Answers1

0

I suggest you use the appearance protocol : https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAppearance_Protocol/index.html

Here is an example of how I customized the appearance of a navigation bar.

// Navigation bar title [[UINavigationBar appearance] setTitleTextAttributes: @{ NSFontAttributeName: [UIFont fontWithName:@"myfont" size:20.0f], NSForegroundColorAttributeName: [UIColor whiteColor] }];

// Navigation bar buttons and background
[UINavigationBar appearance].tintColor = [UIColor whiteColor];
[UINavigationBar appearance].barTintColor = [UIColor teal];
[UINavigationBar appearance].translucent = NO;
Acacio Martins
  • 155
  • 2
  • 10