2011-07-16 122 views
0

我正在開發具有自定義導航欄的基於導航的應用程序。我成功了一個帶有圖像和按鈕的導航欄。但是我不知道如何讓自定義按鈕.. :(ios)Object-c。如何自定義導航欄上的按鈕

這是增加了導航欄上的「主」按鈕的代碼。(在視圖控制器,initWithNibName方法)

if (self) { 
    // Custom initialization. 

    //set navigation id to I to inform that that page is inforview 
    navID = @"I"; 

      //adds the button on the navigation bar 
    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Main" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack:)]; 
    self.navigationItem.leftBarButtonItem = button; 
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault; 
    [button release]; 
    [navID release]; 

} 

謝謝

我創建了一個自定義按鈕,並把它應用到的UIBarButtonItem。 沒有錯誤,但沒有顯示在導航欄:( 上這是我的代碼 -

//create a custom button 
    UIImage *image = [UIImage imageNamed:@"TESTButton.png"]; 
    UIButton *myCustomButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    myCustomButton.bounds = CGRectMake(0, 0, image.size.width, image.size.height);  
    [myCustomButton setImage:image forState:UIControlStateNormal]; 
    [myCustomButton addTarget:nil action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside]; 


    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myCustomButton]; 
    self.navigationItem.leftBarButtonItem = button; 
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault; 

任何人都可以修復我的代碼? :)

回答

1

最簡單的方法是使用:

UIButton *yourCustomButton = .... 
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourCustomButton]; 
self.navigationItem.leftBarButtonItem = barButtonItem; 
[barButtonItem release]; 
+0

嗯,你可以包括yourCustomButton代碼? – Leanne

+0

這取決於你需要什麼。您可以使用'[UIButton buttonWithType:]'創建按鈕並在代碼中進行設置。或者您可以在xib中創建按鈕並將其用於您的自定義'UIBarButtonItem'。沒有「通用」的代碼。 – Andriy