我希望能够设置我的应用程序导航栏后退按钮的字体,而不会做任何太疯狂的事情而不会丢失按钮的任何其他设计特征(即我想保留箭头)。
现在我在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
(root,current等),这都不会对后退按钮进行任何更改。
答案 0 :(得分:112)
要更改所有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];
更新: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)!],
for: .normal)
答案 1 :(得分: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;
popToRoot ViewController:
- (IBAction)popToRoot:(UIBarButtonItem*)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}
也许某人可能会使用此功能。
答案 2 :(得分:4)
上面提到的所有Swift版本(摘自original answer):
let customFont = UIFont(name: "customFontName", size: 17.0)!
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont], forState: .normal)
答案 3 :(得分:2)
在Swift3中:
{
"manifest_version": 2,
"name": "My extension",
"version": "1.0",
"description": "Retrieve local data.",
"homepage_url": "http://Nonefornow",
"icons": {
"48": "icons/beautiful-icon.png"
},
"permissions": [
"activeTab"
],
"browser_action": {
"browser_style": true,
"default_icon": "icons/icon.png",
"default_title": "My Ext",
"default_popup": "popup.html"
}
}
注意:您只需为导航控制器执行一次此操作。
答案 4 :(得分:2)
Swift 3.0 +
<强> AppDelegate.swift 强>
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "myFont", size: 17.0)!], for: .normal)
答案 5 :(得分:1)
在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];
答案 6 :(得分:0)
如果您在iOS 8中使用新的UISplitViewControllerDelegate分割视图,则上述方法无法正常工作,因为新displayModeButtonItem
的工作方式略有不同。
您需要在创建displayModeButtonItem
时设置字体。假设您正在关注Apple的模板,可能会在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];
答案 7 :(得分:0)
如果您需要更改标题,请使用:
UINavigationBar.appearance().titleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.heavy)
]
但如果您的 BarButtonItems
需要它,您可以使用以下内容:
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 17.0)], for: .normal)
<块引用>
注意:这些行将添加到 AppDelegate.Swift
文件