更改后退导航栏button的字体

我希望能够设置我的应用程序导航栏后退button的字体,而不做任何太疯狂,不失去任何其他devise特点的button(即我想保持箭头)。

现在我在viewDidAppear:使用这个viewDidAppear:设置正常的条形button项目的字体。

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

但是,不pipe这个代码应用于哪个UIViewController (root,current等),后退button都不会改变。

要更改所有UINavigationBars出现的所有UIBarButtonItems中的文本外观,请在application:didFinishLaunchingWithOptions:执行以下操作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]; 

更新:iOS7友好版本

 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]; 

迅速:

注意:这将更改UIBarButtonItem所有实例,而不仅仅是包含在UINavigationBar实例

 UIBarButtonItem.appearance() .setTitleTextAttributes([NSFontAttributeName : ExamplesDefaults.fontWithSize(22)], forState: UIControlState.Normal) 

Swift3:UIBarButtonItem.appearance()。setTitleTextAttributes([NSFontAttributeName:UIFont(name:“FontName-Regular”,size:14.0)!],用于:.normal)

对于没有完全得到这个工作的任何人,这里是我如何做到这一点,包括popup到IOS7中的根视图控制器:

 UIBarButtonItem *backBtn =[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:self action:@selector(popToRoot:)]; backBtn.title = @"Back"; [backBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Chalkduster" size:15], NSFontAttributeName, [UIColor yellowColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal]; self.navigationItem.leftBarButtonItem=backBtn; 

popToRoot ViewController:

 - (IBAction)popToRoot:(UIBarButtonItem*)sender { [self.navigationController popToRootViewControllerAnimated:YES]; } 

也许有人可能会使用这个。

以上所有提到的Swift版本(摘自原文的答案 ):

 let customFont = UIFont(name: "customFontName", size: 17.0)! UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont], forState: .normal) 

更多的好东西:
https://stackoverflow.com/a/28347428/469614

在Swift3中:

 let font = UIFont(name: "Verdana", size: 10.0) // Back button UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font!], for: UIControlState.normal) // Title in the navigation item let fontAttributes = [NSFontAttributeName: font] self.navigationController?.navigationBar.titleTextAttributes = fontAttributes 

注意:您只需要为导航控制器执行一次此操作。

在你的AppDelegate或者NavigationController被初始化的地方使用这个方法,在iOS 5及更高版本中可用的方法

 UIBarButtonItem *backbutton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:nil action:nil]; [backbutton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor blackColor],UITextAttributeTextColor,[UIFont fontWithName:TEXTFONT size:16.0f],UITextAttributeFont, nil] forState:UIControlStateNormal]; 

如果您在iOS 8中将新的UISplitViewControllerDelegate用于分割视图,则上述方法将不起作用,因为新的displayModeButtonItem工作方式有点不同。

创builddisplayModeButtonItem时需要设置字体。 假设你正在关注苹果的模板,这可能是在prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender你会做这样的事情:

 // From Apple's Template: NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath]; DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController]; [controller setDetailItem:object]; controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem; controller.navigationItem.leftItemsSupplementBackButton = YES; // New Line to set the font: [controller.navigationItem.leftBarButtonItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"SourceSansPro-Regular" size:14]} forState:UIControlStateNormal];