在iOS 7中,UIBarButtonItems选定的颜色不会正确更改

时间:2013-12-11 10:06:24

标签: ios objective-c uinavigationcontroller uinavigationbar uibarbuttonitem

所以我在iOS 7中遇到了UIBarButtonItem外观的问题。有一个属性,我找不到任何文档,似乎设置了按下时导航栏按钮的不透明度,我不知道如何修改它

[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
[[UIBarButtonItem appearance] setTitleTextAttributes:@{UITextAttributeTextColor: [UIColor whiteColor]} forState:UIControlStateNormal];
[[UIBarButtonItem appearance] setTitleTextAttributes:@{UITextAttributeTextColor: [UIColor orangeColor]} forState:UIControlStateHighlighted];

对于此代码,我得到的结果如下所示。

Normal Button Highlighted Button

我不确定这里发生了什么。第一个问题是我似乎无法得到箭头的色调(因为没有setTintColor:forState:方法)。第二个问题是按下时这种可怕的不透明度/色调。谢谢!

3 个答案:

答案 0 :(得分:1)

//自定义后栏按钮将此代码粘贴到appdelegate

UIImage *buttonBack30 = [[UIImage imageNamed:@"button_back_textured_30"]
                         resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];
UIImage *buttonBack24 = [[UIImage imageNamed:@"button_back_textured_24"]
                         resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 5)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack30
                                                  forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack24
                                                  forState:UIControlStateNormal barMetrics:UIBarMetricsLandscapePhone];

答案 1 :(得分:1)

您需要在ios7中为UIBarButtonItem

实现以下两种方法
@property(nonatomic, retain) UIColor *tintColor
- (void)setBackButtonBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics

答案 2 :(得分:1)

尝试以下可能有效的解决方案

  NSDictionary *attributes = [NSDictionary        dictionaryWithObjectsAndKeys:[UIFont 
fontWithName:@"YOURFONT" size:14], NSFontAttributeName, 
[UIColor whiteColor], NSForegroundColorAttributeName, nil];
  [[UINavigationBar appearance] setTitleTextAttributes:attributes];