2

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

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

设置右栏按钮项

并添加

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

没有帮助,

请帮我摆脱这个背景

在此先感谢,阿米特

4

1 回答 1

1

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

此线程中提供了一种解决方法(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];

虽然这可行,但我不相信这是正确的方法,所以我在这里开始了一个新线程UIToolbar,以防有人知道使包含矩形表现得像透明对象一样的密码。

于 2013-05-17T22:18:05.570 回答