2013-07-03 248 views
2

使用此方法更改後退導航按鈕的標題和背景圖片。標題改變,但按鈕保持黑色更改後退按鈕背景圖片

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"topBarbackground.png"] forBarMetrics:UIBarMetricsDefault]; 

    UIBarButtonItem *backButton = [UIBarButtonItem new]; 
    [backButton setBackgroundImage:[UIImage imageNamed:@"backButton.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 
    [backButton setTitle:NSLocalizedString(@"Back", nil)]; 
    [[self navigationItem] setBackBarButtonItem:backButton]; 
} 

這樣做的正確方法是什麼?

+0

試試下面的代碼... – Jitendra

+0

重複的問題:http://stackoverflow.com/問題/ 10496666/IOS -5-變化最背景的背鍵合導航控制器到透明傳輸 – Mrunal

回答

2

二者必選其一setImagesetBackButtonBackgroundImage因爲setBackgroundImage沒有影響:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"topBarbackground.png"] forBarMetrics:UIBarMetricsDefault]; 

    UIBarButtonItem *backButton = [UIBarButtonItem new]; 
    // Put the image inside the button 
    [backButton setImage:[UIImage imageNamed:@"backButton.png"]; 
    [backButton setTitle:NSLocalizedString(@"Back", nil)]; 
    [[self navigationItem] setBackBarButtonItem:backButton]; 
} 

{ 
    [super viewDidLoad]; 
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"topBarbackground.png"] forBarMetrics:UIBarMetricsDefault]; 

    UIBarButtonItem *backButton = [UIBarButtonItem new]; 
    // Use the image as the button (like custom button) 
    [backButton setBackButtonBackgroundImage:[UIImage imageNamed:@"backButton.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 
    [backButton setTitle:NSLocalizedString(@"Back", nil)]; 
    [[self navigationItem] setBackBarButtonItem:backButton]; 
} 
0
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[backButton addTarget:self action:@selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchUpInside]; 
backButton.frame = kBarBackButtonFrame; 
[backButton setExclusiveTouch:YES]; 
[backButton setBackgroundImage:[UIImage imageNamed:NAV_BAR_BACK_DEFAULT_BUTTON] forState:UIControlStateNormal]; 
[backButton setBackgroundImage:[UIImage imageNamed:NAV_BAR_BACK_DEFAULT_BUTTON] forState:UIControlStateHighlighted]; 
[backButton setBackgroundImage:[UIImage imageNamed:NAV_BAR_BACK_DEFAULT_BUTTON] forState:UIControlStateSelected]; 
[backButton setTitle:NSLocalizedString(@"Back", nil) forState:UIControlStateNormal]; 
backButton.titleLabel.font = [UIFont systemFontOfSize:kBarBackButtonTitleFontSize]; 
[backButton setTitleColor:kBackButtonTitleColor forState:UIControlStateNormal]; 

UIBarButtonItem *backButtonHolder = [[UIBarButtonItem alloc] initWithCustomView:backButton]; 

self.navigationItem.rightBarButtonItem = backButtonHolder; 

請記住,每一個的UIViewController在堆棧中有自己的navigationItem,所以最好的辦法是做一個UIViewController子類,和每次使用的UIViewController,應該延續之前的類。我確信有更好的解決方案,但這是我使用的解決方案。

- (void)popViewControllerWithAnimation { 
[self.navigationController popViewControllerAnimated:YES]; 
}