0

我想創建一個系統UIBarButton,但我希望它具有純色風格。
我試過用這段代碼,但風格被忽略。 它有什麼問題?用UIBarButtonItemStyle創建一個UIBarButtonSystemItem

UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(showSearch)];  
search.style = UIBarButtonItemStylePlain; 
self.navigationItem.rightBarButtonItem = search; 
[search release]; 
+0

'search.style = UIBarButtonItemStylePlain;'是不必要的,因爲'UIBarButtonItemStylePlain'是這個類的風格屬性的默認值。從[UIBarButtonItem_Class](http://developer.apple.com/library/ios/documentation/uikit/reference/UIBarButtonItem_Class/UIBarButtonItem_Class.pdf)。 – chown

+0

我添加它來嘗試刪除邊框。文檔說明式是默認的,但在我的情況中並不是這樣。系統按鈕是有邊界的。 – pasine

回答

0

找到了類似的線程here

看來,制定這個計劃的方法是創建自定義的UIButton並將其分配給bar按鈕。

+1

使用UIButton我失去了避免創建自己的圖像的系統按鈕的優勢。 – pasine

+0

我明白了。我無法驗證這一點,但是我想知道這是否會成爲問題 - >也許系統按鈕默認帶有自己的邊框樣式。如果我有一些時間,我會研究它。 – Madhu

+0

我最終創建了一個圖像。不是我正在尋找的,但它似乎是唯一的解決方案。 – pasine

2

這裏最主要的是你需要將你的按鈕放在前一個控制器中,而不是將要按下的按鈕。

//Your actual ViewController 
UIBarButtonItem *search = [[UIBarButtonItem alloc] 
initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(showSearch)];  
search.style = UIBarButtonItemStylePlain; 
self.navigationItem.rightBarButtonItem = search; 
[search release]; 

//Controller that is going to be pushed and will display the new UIBarButtonItem 
[self.navigationController pushViewController:newViewController animated:YES];