custom right bar button item in ios6 is not transparent

三世轮回 提交于 2019-12-06 11:32:04

问题


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

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

and adding

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

doesn't help,

please help me get rid of this background

Thanks in advance, Amit


回答1:


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

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

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.



来源:https://stackoverflow.com/questions/14926186/custom-right-bar-button-item-in-ios6-is-not-transparent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!