2011-12-22 138 views
0

我需要添加一個透明的工具欄,並添加4個圖像的按鈕。我怎樣才能通過代碼做到這一點?將按鈕添加到工具欄

我的工作到目前爲止;

我已添加工具欄,並使其透明 現在我正試圖添加4個按鈕的圖像。我怎樣才能做到這一點 ? (這4個按鍵也有action方法,所以當一個人點擊它的動作應該火起來)

toolBar1 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 320 , 320 , 60)]; 

[toolBar1 setBarStyle:UIBarStyleBlack]; 

[toolBar1 setTranslucent:YES]; 

NSMutableArray* allthebuttons = [[NSMutableArray alloc] initWithCapacity:4]; 

UIBarButtonItem *buttonWithImage = [[UIBarButtonItem alloc] ...... // Now what ?? 

[self.view addSubview:toolBar1]; 

回答

0
NSMutableArray *buttonsArray = [[NSMutableArray alloc] init]; 
UIBarButtonItem *myButton1=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"email.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toolbarButtonPressed1:)]; 
[buttonsArray addObject:myButton1]; 
UIBarButtonItem *myButton2 = [[UIBarButtonItem alloc] initWithTitle:@"button 2" style:UIBarButtonItemStylePlain target:self action:@selector(toolbarButtonPressed2:)]; 
[buttonsArray addObject:myButton2]; 
[self setToolbarItems:buttonsArray animated:YES]; 

請注意,對於上述,您需要使用導航視圖控制器上的工具欄。

+0

好的,但按鈕中的圖像在哪裏?我也需要添加圖片。我不希望標題我只想要顯示圖像 – sharon 2011-12-22 16:51:39

+0

啊,錯過了,現在更新的代碼。 – ader 2011-12-22 16:54:41

+0

嗯...爲什麼我沒有想到這一點。謝謝你的加載 – sharon 2011-12-22 16:58:07

1
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered target:self action:@selector(action]; 

UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"Item1" style:UIBarButtonItemStyleBordered target:self action:@selector(action]; 

NSArray *buttons = [NSArray arrayWithObjects: item1, item2, nil]; 
[toolBar setItems: buttons animated:NO]; 

[item1 release]; 
[item2 release]; 

只是對上面的代碼爲4個按鈕

更新1:

使用下面的代碼獲取圖像按鈕

UIImageView *btn1Img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Btn1Img"]]; 

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:btn1Img]; 
+0

好的,但按鈕中的圖像在哪裏?我也需要添加圖片。我不想要標題我只想顯示圖像 – sharon 2011-12-22 16:52:36

+0

我做了upvote,但是@ade首先改變了代碼:S無論如何謝謝加載:* – sharon 2011-12-22 16:59:40

+0

別擔心我會很快發佈更多問題。確保你先回答它:D – sharon 2011-12-22 17:32:28