更改后导航栏按钮的字体

zac*_*hjs 47 objective-c uinavigationbar uibarbuttonitem uifont ios

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

现在我用它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]];
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,这不会对后退按钮进行任何更改,无论UIViewController此代码应用于哪个(根,当前等).

Mik*_*ard 114

要更改所有文本中UIBarButtonItems出现的文本外观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];
Run Code Online (Sandbox Code Playgroud)

更新: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];
Run Code Online (Sandbox Code Playgroud)

迅速:

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

UIBarButtonItem.appearance()
               .setTitleTextAttributes([NSFontAttributeName : ExamplesDefaults.fontWithSize(22)], 
                                       forState: UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud)

Swift3:

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


Pet*_*erK 6

对于那些没有完全解决这个问题的人来说,我就是这样做的,包括弹回到IOS7中的Root ViewController:

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;
Run Code Online (Sandbox Code Playgroud)

popToRoot ViewController:

- (IBAction)popToRoot:(UIBarButtonItem*)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}
Run Code Online (Sandbox Code Playgroud)

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