自定义UIBarButtonItem initWithBarButtonSystemItem在iOS 5中显示为灰色而不是白色

时间:2013-11-12 17:15:20

标签: iphone ipad uibarbuttonitem ios5.1

我在UINavigationBar上使用带有darkGrayColor文本的白色背景

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"uinavigationcontrollerbackground"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,UITextAttributeFont ,[UIColor darkGrayColor], UITextAttributeTextColor,  [UIColor whiteColor], UITextAttributeTextShadowColor, nil]];

看起来很不错但我在我的应用程序中有这样的按钮:

self.searchButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchProducts)];

我杀了默认的blakc背景看起来像这样:

[[UIBarButtonItem appearance] setBackgroundImage:[UIImage imageNamed:@"uibarbuttonbackground"]  forState:UIControlStateNormal
                                          barMetrics:UIBarMetricsDefault];

这只是一个白色方块。

现在我的图标仍为白色,带有浅灰色阴影,无论如何要更改这些默认图标?

2 个答案:

答案 0 :(得分:0)

一旦我尝试更改UIBarButtonSystemItemCamera的背景但没有任何效果。我想这些默认系统按钮类型(UIBarButtonSystemItem)已经带有背景图像,因此更改它不会。

我建议你创建一个自定义视图并使用它启动你的栏按钮。不是一个完美的代码,但我希望它有所帮助。

 UIImage *image = [UIImage imageNamed:@"icon.png"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage: [image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal];
    button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    [button addTarget:self action:@selector(takePicture:)    forControlEvents:UIControlEventTouchUpInside];

    UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
    [v addSubview:button];


    UIBarButtonItem *barItem = [[[UIBarButtonItem alloc] initWithCustomView:v] autorelease];

答案 1 :(得分:0)

我使用了这里找到的类别解决方案,它运行得很好:

UIBarButtonItem with custom image and no border

我的工作量比我希望的要多,但这是一个很好的清洁解决方案。