我希望能够设置我的应用程序导航栏后退按钮的字体,而不会做任何太疯狂的事情,也不会丢失按钮的任何其他设计特征(即,我希望保留箭头).

现在我在viewDidAppear:中使用它来设置普通条按钮项目的字体.

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

但是,无论该代码应用于哪个UIViewController(根、当前等),后退按钮都不会发生变化.

推荐答案

要更改all UIBarButtonItems中出现在all UINavigationBars中的文本的外观,请在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];

Swift :

注意:这会更改UIBarButtonItem的所有实例,而不仅仅是UINavigationBar中包含的实例

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

迅捷3:

UIBarButtonItem.appearance()
     .setTitleTextAttributes([NSFontAttributeName: UIFont(name: "FontName-Regular", size: 14.0)!], 
                             for: .normal) 

Objective-c相关问答推荐

Objective C 布尔数组

自构建预编译头后文件已被修改

检测时间格式是 12 小时还是 24 小时格式

使用正则表达式搜索 NSString

如何在 iPhone 应用程序的 UINavigationBar 中添加图像

静态和动态单元格的 UITableView 混合?

如何处理 Xcode 警告没有以前的函数原型......?

为什么我的 UIButton 的内部修饰事件没有被调用?

与 iOS 6.0 原生 Facebook 集成共享:通过我的应用名称发布?

从我在 iPhone 上的应用程序调用官方 *Settings* 应用程序

更改 UIBarButtonItem 的 Tint colored颜色

UICollectionView 单元格 Select 和单元格重用

仅在 UIView 顶部的圆角

自定义 colored颜色 我的 UIActivityIndi​​catorView

Objective-c 中 SEL 类型的属性

导航返回时重新加载 UITableView?

发送通知到山狮通知中心

NSString 中子字符串的出现次数?

如何在没有死锁的情况下同步调度主队列?

找出窗口中的 UIBarButtonItem 框架?