2012-09-13 221 views
0

我向故事板中的tableView添加了右側欄按鈕,創建了一個打開另一個窗口的IB動作。一切正常。但是現在我想爲該按鈕使用自定義圖像。我發現這一點:自定義右側導航欄按鈕

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button setImage:infoIconImage forState:UIControlStateNormal]; 

我的猜測與代碼的一些額外的線路將做的工作,但 我似乎無法理解該按鈕在代碼什麼名字。我如何將故事板中的按鈕與代碼相關聯。

初學者問題,對不起。

回答

0

創建一個IBOutlet,就像將您的動作從故事板連接到.h文件一樣。將它命名爲按鈕,你很好走!

順便說一句,您可以在故事板中設置「標準」圖像:選擇按鈕,轉到Xcode的實用工具欄中的屬性檢查器(右欄),然後更改標識符!

編輯:

看一看那邊使用自己的形象:

Customize UIBarButtonItem

有關的UIBarButtonItem的部分是靠近文章的末尾。

+0

謝謝,這回答我的問題,但引發了另一個。它創建了一個UIBarButtonItem類型。任何想法如何定製它(使用自定義圖像)?上面的代碼片段處理UIButton,而不是... –

+0

看到我的自定義圖像的更新,以防您發佈的解決方案不起作用:) – rdurand

+0

我在下面發佈的代碼製作得很好,自定義按鈕在那裏,但對於一些理由行動不工作了... –

0

好的,我發現有一個屬性customView。所以在這裏它是:

// Set the custom back button 
UIImage *infoIconImage = [UIImage imageNamed:@"info-button.png"]; 
//create the button and assign the image 
UIButton *iButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[iButton setImage:infoIconImage forState:UIControlStateNormal]; 

//set the frame of the button to the size of the image 
iButton.frame = CGRectMake(0, 0, infoIconImage.size.width, infoIconImage.size.height); 

//assigning customized button 
infoButton.customView = iButton; 
+0

沒有人有一個想法,爲什麼當我將iButton分配給在故事板中創建的我的infoButton時,推動動作被禁用?我懷疑走向UIControlStateNormal。 –

+0

它不是* UIControlStateNormal *,因爲這只是告訴按鈕你傳遞的圖像(infoIconImage)是* normal *狀態按鈕的圖像。所以在這裏沒有問題,我想..我會讓你知道,如果一個想法來到我的腦海裏.. – rdurand

+0

謝謝,我還沒有解決它呢 –