在uinavigationcontroller上更改后退按钮的字体
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在uinavigationcontroller上更改后退按钮的字体相关的知识,希望对你有一定的参考价值。
我正在尝试更改UINavigationControllerBar中后退按钮上文本的字体颜色
[[UIBarButtonItem appearance] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
给我这个错误:[_ UIBarItemAppearance setTitleColor:forState:]:无法识别的选择器发送到实例0x69aeb70'
有帮助吗?谢谢!
答案
在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];
另一答案
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setValue:[UIColor colorWithRed:(163.0f/255.0f) green:(0.0f) blue:(0.0f) alpha:1.0f] forKey:UITextAttributeTextColor];
[attributes setValue:[UIColor clearColor] forKey:UITextAttributeTextShadowColor];
[attributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
似乎工作!
另一答案
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:kDefaultFont size:16.0f],UITextAttributeFont,
nil] forState:UIControlStateNormal];
另一答案
而美丽的解决方案,iOS7 +(因为属性名称):
NSShadow *shadow = [NSShadow new];
[shadow setShadowColor: [UIColor colorWithWhite:0.0f alpha:0.750f]];
[shadow setShadowOffset: CGSizeMake(0.0f, 1.0f)];
[[UIBarButtonItem appearance] setTitleTextAttributes:@{
NSFontAttributeName: [UIFont systemFontOfSize:24],
NSForegroundColorAttributeName: [UIColor colorWithWhite:0.2 alpha:1.0],
NSShadowAttributeName: shadow,
} forState:UIControlStateNormal];
另一答案
Swift 4中的解决方案:
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSAttributedStringKey.font: UIFont(name: "MyriadPro-SemiboldCond", size: 16)!,
NSAttributedStringKey.foregroundColor: UIColor.white
], for: .normal)
在AppDelegate中添加它,它将应用于应用程序中的所有按钮。
以上是关于在uinavigationcontroller上更改后退按钮的字体的主要内容,如果未能解决你的问题,请参考以下文章