ios6中的自定义右键按钮项不透明 [英] custom right bar button item in ios6 is not transparent

查看:107
本文介绍了ios6中的自定义右键按钮项不透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码为导航栏创建自定义右侧栏按钮项:

i am creating a custom right bar button item for my navbar using the following code :

// create a toolbar 
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 50, 44.01)];
// create the array to hold the button, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:1];

// create a toolbar item with image
NSString* pathToSettingsResourceFile = [[NSBundle mainBundle] pathForResource:@"19-gear" ofType:@"png"];        
UIImage* settingsImage = [[[UIImage alloc] initWithContentsOfFile:pathToSettingsResourceFile] autorelease];         
settings = [[UIBarButtonItem alloc]initWithImage:settingsImage style:UIBarButtonItemStylePlain target:self action:@selector(showSettings:)];
settings.enabled = FALSE;

// add button to toolbar 
[buttons addObject:settings];
[tools setItems:buttons animated:NO];
[buttons release];

// add toolbar as a right bar button item 
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:tools] autorelease];

[tools release];   

直到ios 6按预期工作,从ios 6我得到这个奇怪的矩形背景

until ios 6 this works as expected, from ios 6 i am getting this weird rectangle background

并添加

tools.opaque = NO;
tools.backgroundColor = [UIColor clearColor];

无效,

please帮我摆脱这个背景

please help me get rid of this background

提前致谢,
Amit

Thanks in advance, Amit

推荐答案

我知道这是一个老线程,但我遇到了同样的问题,并且认为你可能对我发现的内容感兴趣。

I know this is an old thread, but I had the same issue, and thought you might be interested in what I discovered.

此线程中提供了解决方法(jd的答案,而不是已接受的答案)。

A work-around is available in this thread (the answer by jd, not the "accepted" answer).

您应该能够通过以下代码获得所需的行为:

You should be able to get the behavior you want with this code:

const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];

[tools setBackgroundImage:maskedImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

虽然这有效,但我不相信它是正确的方式要做到这一点,所以我开始这里有一个新主题知道使包含矩形的 UIToolbar 的密码行为应该是透明对象。

Although this works, I am not convinced that it is the correct way to do it, so I started a new thread here in case anyone knows the secret code for making a UIToolbar containing rectangle behave as a transparent object should.

这篇关于ios6中的自定义右键按钮项不透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆