我已經有了這個代碼永遠,它曾經工作,但它開始展示一個惱人的行爲,我認爲,iOS 6。有幾個類似的線程在SO,但我避難所我還沒有找到一個我對此感到滿意的解決方案。我找到了一個解決方法(見下文),但它看起來並不像「正確」的方式。UIImage UIToolbar按鈕不完全透明
Here's a thread with the same issue, but no answers.
當我使用的圖像作爲UIBarButtonItem
,放入一個UIToolbar
,所述UIToolbar
的含有矩形並不完全透明的。我用於UIBarButtonItem
的PNG圖像是透明的,所以這不是問題。這裏有一個例子:
當我使用的不是圖像爲UIBarButtonItem
文本,透明度按預期工作。像這樣:
這是因爲永遠已經工作的代碼:
NSMutableArray *buttonItems = [[[NSMutableArray alloc] initWithCapacity: 1] autorelease];
UIImage *printButtonImage = [UIImage imageNamed:@"buttonPrint.png"];
UIToolbar *toolBarButtons = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 52.0, 44.01)] autorelease];
UIBarButtonItem *printButton = [[[UIBarButtonItem alloc] initWithImage:printButtonImage style:UIBarButtonItemStyleBordered target:self action:@selector(printDocument:)] autorelease];
[buttonItems addObject:printButton];
[toolBarButtons setItems:buttonItems animated:YES];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:toolBarButtons] autorelease];
據SO的建議,我剛纔之前[toolBarButtons setItems...]
補充這一點,但它並沒有影響:
toolBarButtons.backgroundColor = [UIColor clearColor];
[toolBarButtons setTranslucent:YES];
[toolBarButtons setOpaque:NO];
I found 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)];
[toolBarButtons setBackgroundImage:maskedImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
我也看到一對夫婦對這個問題需要得到navigationController
的頂部或底部的顏色和應用梯度的解決方案,但我不相信,有沒有辦法使矩形的UIToolbar
完全透明,其中應該允許navigationController
顏色通過,漸變和所有。
正在申請遮掩圖像真的模擬正確的UI體驗的唯一途徑?有蘋果真的沒有提供一種方法來簡單地使UIToolbar
包含矩形完全透明?
現在,我有正確的行爲,但我不覺得我有正確的解決方案。這感覺像一個黑客。有沒有人知道有更好的方法來實現這一目標?
對不起,你爲什麼把一個UIToolbar放在UINavigationBar中?爲什麼不直接添加UIBarButtonItem到UINavigationBar(通過'navigationItem')? – matt
@matt因爲通過按下按鈕訪問的功能不是導航堆棧的任何部分。給出的具體例子是打印對話框。 [UIViewController的navigationItem引用](https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html):「...你不應該訪問這個屬性,如果你是不使用導航控制器來顯示視圖控制器。「 –
即使沒有導航控制器,也可以使用導航欄和導航項目。即使你從不打算導航,你也可以使用導航控制器,就像獲得導航欄的簡單方法一樣。這都是常見的和合法的。什麼是不尋常的,非法的,不必要的是你正在做什麼,在導航欄中粘貼一個工具欄。 – matt