简体   繁体   中英

How to change font of navigation items?

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?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM