点击时,iOS 7 UIBarButtonItem字体发生变化 [英] iOS 7 UIBarButtonItem font changes when tapped
问题描述
我试图改变我的 UIBarButtonItem
字体。 ViewControllers加载时看起来不错。但是,如果我点击栏按钮,或者向右滑动,就好像移动到前一个ViewController(但是然后拉回到当前的那个),字体将变回到系统字体。以下是我在AppDelegate中设置的内容:
pre $ {code $ NSDictionary * barButtonItemAttributes = @ {NSFontAttributeName:[UIFont fontWithName:@SourceSansPro-光大小:20.0f]};
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonItemAttributes forState:UIControlStateNormal];
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonItemAttributes forState:UIControlStateHighlighted];
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonItemAttributes forState:UIControlStateSelected];
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonItemAttributes forState:UIControlStateDisabled];
这里是我的viewWillAppear的一个例子:
- (void)viewWillAppear:(BOOL)animated {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@Donestyle:UIBarButtonItemStylePlain target:self action :@selector(doneButtonPressed)];
self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:141.0 / 255.0 green:209.0 / 255.0 blue:205.0 / 255.0 alpha:1.0];
}
我以某种方式改变了字体,还是滥用了外观代理?问题是 tintColor
设置与标题文本冲突属性。如果您要使用标题文本属性,则将文本颜色合并到这些属性中:
$ b
NSDictionary * barButtonItemAttributes =
@ {NSFontAttributeName:
[UIFont fontWithName:@Georgiasize:20.0f],
NSForegroundColorAttributeName:
[UIColor colorWithRed:141.0 / 255.0 green:209.0 / 255.0 blue:205.0 / 255.0 alpha:1.0]
};
I'm attempting to change my UIBarButtonItem
font. It looks good when ViewControllers load. But if I tap on the bar button, or swipe right as if to move to the previous ViewController (but then pull back to the current one), the font changes back to the system font. Here's what I'm setting in my AppDelegate:
NSDictionary* barButtonItemAttributes = @{NSFontAttributeName: [UIFont fontWithName:@"SourceSansPro-Light" size:20.0f]};
[[UIBarButtonItem appearance] setTitleTextAttributes: barButtonItemAttributes forState:UIControlStateNormal];
[[UIBarButtonItem appearance] setTitleTextAttributes: barButtonItemAttributes forState:UIControlStateHighlighted];
[[UIBarButtonItem appearance] setTitleTextAttributes: barButtonItemAttributes forState:UIControlStateSelected];
[[UIBarButtonItem appearance] setTitleTextAttributes: barButtonItemAttributes forState:UIControlStateDisabled];
And here's an example of my viewWillAppear:
- (void) viewWillAppear:(BOOL)animated {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(doneButtonPressed)];
self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:141.0/255.0 green:209.0/255.0 blue:205.0/255.0 alpha:1.0];
}
Am I somehow changing the font back, or am I misusing the appearance proxy?
The problem is that the tintColor
setting conflicts with the title text attributes. Comment out that line and all will be well.
If you are going to use title text attributes, then incorporate the text color into those attributes:
NSDictionary* barButtonItemAttributes =
@{NSFontAttributeName:
[UIFont fontWithName:@"Georgia" size:20.0f],
NSForegroundColorAttributeName:
[UIColor colorWithRed:141.0/255.0 green:209.0/255.0 blue:205.0/255.0 alpha:1.0]
};
这篇关于点击时,iOS 7 UIBarButtonItem字体发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!