2012-08-16 33 views
0

我編程包括動作按鈕並用下面的代碼工具欄上的郵件按鈕:使UIBarButtonSystemItems出現在圓矩形按鈕,而不是僅僅靠自己

UIBarButtonItem *compose = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:7 target:self action:@selector(userWritesHaiku)]; 

compose.style=UIBarButtonItemStyleBordered; 

UIBarButtonItem *action = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:9 target:self action:@selector(userWritesHaiku)]; 

action.style=UIBarButtonItemStyleBordered; 

(然後將它們放在一個陣列,並將它們分配給工具欄)

但是,這給了我下面的輸出:

buttons created programmatically

我想要的是以下內容,我可以使用Interface Builder創建這些內容,但不使用Interface Builder。

buttons created with Interface Builder

我怎樣才能得到後者的圖像編程?

回答

1

該物品的style屬性的默認值爲UIBarButtonItemStylePlain。您需要將其設置爲UIBarButtonItemStyleBordered

我試圖將此代碼的iOS 5.0模擬器:

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(self)]; 
//item.style = UIBarButtonItemStyleBordered; 
self.toolbar.items = [self.toolbar.items arrayByAddingObject:item]; 

我得到了這樣的結果:

plain UIBarButtonItem

然後,我把它改成下面的代碼:

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(self)]; 
item.style = UIBarButtonItemStyleBordered; 
self.toolbar.items = [self.toolbar.items arrayByAddingObject:item]; 

我得到了這個結果:

bordered UIBarButtonItem

+0

是的,這就是我想在第一次 - 但如果你在我發佈的代碼看,你會發現其實我設置樣式爲邊界 - 但它沒有做任何我好。 :( – 2012-08-16 03:03:09

+0

我認爲你必須做些別的事情來打破它,我已經更新了我的答案 – 2012-08-16 03:22:37

+0

好吧,在我修好之後,它的工作原理 - 我認爲我做了一些愚蠢的事情,例如用一種方法設置按鈕樣式,但然後從另一種方法添加按鈕,但我太尷尬,以肯定的發現。非常感謝您的幫助! – 2012-08-16 03:35:44