【问题标题】:change navigation bar button item color in ios6在 ios6 中更改导航栏按钮项目的颜色
【发布时间】:2014-09-13 01:23:56
【问题描述】:

我想为iOS6更改导航栏后退按钮的颜色,我尽力了,但没有成功。那么请谁能帮我找到完美的解决方案?

我想让这个按钮的背景变黑。

我用过

[[UIBarButtonItem appearance]setTintColor:[UIColor blackColor]];

但它不起作用。我也用过这个:

[[[self navigationController] navigationBar] setBarTintColor:[UIColor blackColor]]; 

但出现此错误:

Terminating app due to uncaught exception
NSInvalidArgumentException', reason: '-[UINavigationBar setBarTintColor:]: unrecognized selector sent to instance 0x9d2ab30

【问题讨论】:

  • 仍然支持 iOS 6 吗?考虑到Apple's latest numbers 表示 90% 的用户都在使用 iOS 7,这一定很痛苦,一旦 iOS 8 出来,iOS 6 的市场份额很可能会更低......
  • 是的,我知道,但我的客户想要 ios6 和 ios7 中的应用程序。
  • 哦,谢谢,如果您有解决方案,请告诉我...
  • 您只想更改后退按钮颜色或整个导航栏?

标签: ios iphone objective-c ios7 ios6


【解决方案1】:

iOS 6

[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];

在 iOS7 中

[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];

另一种选择

//allocating the bar button
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(popToBack)];

 //assign the bar button to the navigation bar

self.navigationItem.leftBarButtonItem = leftBarButton;

//change the tint color of the bar button item
self.navigationItem.leftBarButtonItem.tintColor=[UIColor colorWithRed:125.0/255.0 green:90.0/255.0 blue:146.0/255.0 alpha:1];  //[UIColor blackColor]


-(void)popToBack
{
[self.navigationController popViewControllerAnimated:YES];
 }

【讨论】:

  • 你需要更改栏按钮背景颜色
  • @user3824002 我更新了我的答案试试这个,我在等你的回复
【解决方案2】:

试试这个

// UIBarItem 外观

 [[UIBarButtonItem appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
 [UIColor colorWithRed:140.0f/255  green:0.0 blue:0.0 alpha:1.0], UITextAttributeTextColor,
 [UIColor whiteColor], UITextAttributeTextShadowColor,
 [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
 [UIFont fontWithName:@"Helvetica neue" size:16.0], UITextAttributeFont,
 nil]
 forState:UIControlStateNormal];

//使用图片

[[UIBarButtonItem appearance] setBackgroundImage:doneBackgroundImage
                                            forState:UIControlStateNormal
                                               style:UIBarButtonItemStyleDone
                                          barMetrics:UIBarMetricsDefault];

【讨论】:

  • 没有其他解决方案?感谢重播
  • 您也可以尝试使用自定义图像,但此代码应该可以工作!我将其添加到我的答案中
【解决方案3】:

试试这个

  CGRect rectQuestion=CGRectMake(0 , 0, 50, 30);
    UIButton *btnLeft=[[UIButton alloc]init];
    [btnLeft setFrame:rectQuestion];
    [btnLeft setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
    [btnLeft setBackgroundColor:[UIColor blackColor]];
    [btnLeft removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
    [btnLeft addTarget:self action:@selector(PopThis) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBar=[[UIBarButtonItem alloc]initWithCustomView:btnLeft];
    self.navigationItem.leftBarButtonItem=leftBar;
    [btnLeft release];
    [leftBar release];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多