2009-08-06 99 views
2

我想將自定義箭頭按鈕添加到頂部的UINavigation Bar。如何將自定義圖像按鈕添加到iPhone中的UINavigationBar sdk

我想要做的是創建一個自定義圖像按鈕,並將其放置在酒吧。當我嘗試添加,查看,如:

[self.view addSubview:leftArrowButton];//(Where leftArrowButton is a UIButton with an image of arrow) 

這不工作,就像它把按鈕,但按鈕被紮下牆根,並在其上不重疊。

任何幫助表示讚賞。謝謝。

回答

7

嘗試執行以下操作:

UINavigationBar *bar = self.navigationController.navigationBar; 
bar.titleView = leftArrowButton; // change title view 
bar.leftBarButtonItem = leftArrowButton; // if ref is UIBarButtonItem 

你應該考慮使用UIBarButtonItem而非UIButton

您還可以自定義欄按鈕以及與圖像(使用initWithImage:style:target:action:):使用

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:myImage 
          style:UIBarButtonItemStyleBordered 
          target:self action:@selector(buttonPressed:)]; 
bar.leftBarButtonItem = item; 
[item release]; 

您也可以使用任何你喜歡的視圖(有一些考慮大小)的initWithCustomView:

UIBarButtonItem *item = [[UIBarButtonItem alloc] 
          initWithCustomView:leftArrowButton]; 
bar.leftBarButtonItem = item; 
[item release]; 
+0

我知道我可以使用barButtonItem,但我不能自定義按鈕。我想定製按鈕。 – rkb 2009-08-06 15:50:05

+0

更新後的文章介紹如何自定義'UIBarButtonItem'。 – notnoop 2009-08-06 16:28:01

相關問題