ios6中的自定义右键按钮项不透明

时间:2013-02-17 21:20:39

标签: ios6 uinavigationbar rightbarbuttonitem

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

// 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我得到这个奇怪的矩形背景

settings right bar button item

并添加

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

无济于事,

请帮我摆脱这个背景

提前致谢, 阿米特

1 个答案:

答案 0 :(得分:1)

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

A work-around is available in this thread(jd的答案,而不是“接受”的答案)。

您应该能够使用此代码获得所需的行为:

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

虽然这有效,但我不相信这是正确的方式,所以我开始a new thread here,以防有人知道制作{{1}的密码包含矩形的行为应该是透明对象。

相关问题