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