点击时,iOS 7 UIBarButtonItem字体会发生变化

我正在尝试更改我的UIBarButtonItem字体。 ViewControllers加载时看起来不错。 但是如果我点击条形按钮,或向右滑动就像移动到上一个ViewController(但然后拉回到当前的那个),字体会变回系统字体。 这是我在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]; 

这是我的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]; } 

我是以某种方式更改字体,还是我滥用外观代理?

问题是tintColor设置与标题文本属性冲突。 注释掉那条线,一切都会好的。

如果要使用标题文本属性,请将文本颜色合并到这些属性中:

 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] }; 

通过在viewDidLoad方法中使用此方法,您可以一次性设置自定义文本,字体,大小等。

 UIView *customBarButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)]; UIButton *buttonDone = [UIButton buttonWithType:UIButtonTypeSystem]; [buttonDone addTarget:self action:@selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; buttonDone.frame = CGRectMake(10, 0, 50, 30); buttonDone.titleLabel.textColor = [UIColor colorWithRed:141.0/255.0 green:209.0/255.0 blue:205.0/255.0 alpha:1.0]; buttonDone.titleLabel.font = [UIFont fontWithName:@"SourceSansPro-Light" size:20.0f]; [buttonDone setTitle:@"Done" forState:UIControlStateNormal]; [customBarButtonView addSubview:buttonDone]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:customBarButtonView]; 
 _myrButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"French KeyBoard" style:UIBarButtonItemStylePlain target:self action:@selector(RbuttonItemdown:)]; NSDictionary* itemTextAttributes = @{NSFontAttributeName:[UIFont fontWithName:@"Georgia" size:12.0f], NSForegroundColorAttributeName:[UIColor blueColor], NSBackgroundColorAttributeName:[UIColor lightGrayColor]}; [_myrButtonItem setTitleTextAttributes:itemTextAttributes forState:UIControlStateNormal]; [self.navigationItem setRightBarButtonItem:_myrButtonItem]; 

你可以使用以下所有这些:

 NSString *const NSFontAttributeName ; NSString *const NSParagraphStyleAttributeName ; NSString *const NSForegroundColorAttributeName ; NSString *const NSBackgroundColorAttributeName ; NSString *const NSLigatureAttributeName ; NSString *const NSKernAttributeName ; NSString *const NSStrikethroughStyleAttributeName ; NSString *const NSUnderlineStyleAttributeName ; NSString *const NSStrokeColorAttributeName ; NSString *const NSStrokeWidthAttributeName ; NSString *const NSShadowAttributeName ; NSString *const NSTextEffectAttributeName ; NSString *const NSAttachmentAttributeName ; NSString *const NSLinkAttributeName ; NSString *const NSBaselineOffsetAttributeName ; NSString *const NSUnderlineColorAttributeName ; NSString *const NSStrikethroughColorAttributeName ; NSString *const NSObliquenessAttributeName ; NSString *const NSExpansionAttributeName ; NSString *const NSWritingDirectionAttributeName ; NSString *const NSVerticalGlyphFormAttributeName;