2013-02-17 37 views
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中我是得到這個怪異的矩形背景

settings right bar button item

並添加

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

沒有幫助,

請幫我擺脫這種背景下提前

感謝, 阿米特

回答

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任何人都知道使包含矩形的UIToolbar的祕密代碼應該像透明對象那樣工作。