2012-01-27 76 views
0

從文檔UIBarButtonItems customViews,我認爲這對於backBarButtonItem:是忽略了leftBarButtonItem

當此屬性是零,導航項目在其 標題使用值(第10頁)屬性創建一個合適的後退按鈕。如果您要 想要爲後退按鈕指定自定義圖像或標題,則可以使用 指定 此屬性的自定義欄按鈕項目(使用您的自定義標題或圖像)。配置您的酒吧按​​鈕項目時,不要 爲其分配自定義視圖;導航項目無論如何都會忽略後欄按鈕中的自定義視圖 。

我不知道這是否與leftBarButtonItem相同?基本上我有這個代碼:

UIButton *homeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)]; 
UIImage *homeImage = [UIImage imageNamed:@"icon_house.png"]; 
[homeButton setImage:homeImage forState:UIControlStateNormal]; 
[homeButton addTarget:self action:@selector(homePressed:) forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *homeBBI = [[UIBarButtonItem alloc] initWithCustomView:homeButton]; 

在iOS 5之前,我把它放在一個UIToolBar中,它表現良好。現在我想把它作爲leftBarButtonItem,放在UINavigationController的backButton的右邊。當我設置它時,它根本不顯示。沒有圖像。但是,當我創建這樣的按鈕:

UIBarButtonItem *hButton = [[UIBarButtonItem alloc] initWithTitle:@"home" style:UIBarButtonItemStylePlain target:self action:@selector(homePressed:)]; 

並將其設置爲leftBarButtonItem,它顯示了。我不知道如何讓我的自定義圖標爲我的主頁按鈕沒有邊界。當我使用:

UIBarButtonItem *hButton2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_house.png"] style:UIBarButtonItemStylePlain target:self action:@selector(homePressed:)]; 

我得到一個我的房子周圍我不想要的邊框。謝謝。

回答

2

試試這個

UIButton *TastoVersamento = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [TastoVersamento setImage:[UIImage imageNamed:@"Versamento.png"] forState:UIControlStateNormal]; 
     [TastoVersamento addTarget:self action:@selector(Click_Versamento:) forControlEvents:UIControlEventTouchUpInside]; 
     [TastoVersamento setFrame:CGRectMake(0, 0, 40, 40)]; 
     [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:TastoVersamento] autorelease]]; 
0

backBarButtonItemleftBarButtonItem是不同的。 backBarButtonItem是默認的,如果沒有給出leftBarButtonItem。即使leftItemsSupplementBackButtonYES,即使設置了leftBarButtonItem也會顯示。

在第一個示例中,您可以嘗試[homeButton sizeToFit]。我認爲框架可能沒有正確設置。

但是至於你最後的評論,我認爲你將不會成功從導航項目按鈕中刪除邊框。我很確定他們是硬編碼的。事實上,如果你添加一個有邊框的按鈕,你會得到兩個,一個來自按鈕,一個來自導航項目。

+0

界面污物。你提前設定框架......無論如何,我認爲你的第二個例子是你想要的:'initWithImage'。 (按鈕內的按鈕很奇怪。)'initWithImage'就是我所做的。正如我所說,我不相信你能夠刪除邊界。 – smparkes 2012-01-27 17:02:12