2011-10-27 59 views
0

我正在用自定義背景圖像和標題創建自定義UINavigationItem。圖像顯示,但標題不顯示。什麼可能是錯的? 我做這樣說:自定義UINavigationItem上的標題未顯示

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button setImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal]; 
[button setBackgroundImage:[UIImage imageNamed:@"bar_button_black.png"] forState:UIControlStateSelected]; 
[button setBackgroundImage:[UIImage imageNamed:@"bar_button_black.png"] forState:UIControlStateHighlighted]; 
[button setTitle:@"Cancel" forState:UIControlStateNormal]; 
[button setTitle:@"Cancel" forState:UIControlStateSelected]; 
[button setTitle:@"Cancel" forState:UIControlStateHighlighted]; 
[button addTarget:self action:@selector(closeButtonClicked) forControlEvents:UIControlEventTouchUpInside]; 

[button setFrame:CGRectMake(0, 0, 49, 30)]; 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 

回答

0

您需要將圖像設置爲backgroundImage不直接。如果設置圖像這樣

[button setImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal]; 

標題將不會顯示

一套這樣

[button setBackgroundImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal]; 
+0

明白了。謝謝! – Misha

0

我認爲你必須刪除:

[button setImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal]; 

之後,標題將出現

0

要麼刪除此行

[button setImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal]; 

或者您可以將標題顏色更改爲其他顏色。我希望它能解決您的問題。

使它成爲DrakgrayColor或其他可以克服你的圖像顏色的顏色。

+0

想必移除圖像不是一個選項,然後單擊按鈕看起來是錯誤的。改變標題顏色會更好。 – jrturton

+0

改變顏色沒有幫助。 – Misha

0

好的。找到了解決方法,但我不確定是否允許:

UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close",@"Close") style:UIBarButtonItemStylePlain target:self action:@selector(closeButtonClicked)]; 
[self.navigationItem setLeftBarButtonItem:closeButton]; 

for (UIView *view in self.navigationController.navigationBar.subviews) {   
    if ([[[view class] description] isEqualToString:@"UINavigationButton"]) { 
     UINavigationButton *button = {(UINavigationButton *)view}; 
     [button setBackgroundImage:[UIImage imageNamed:@"bar_button.png"] forState:UIControlStateNormal]; 
     [button setBackgroundImage:[UIImage imageNamed:@"bar_button_black.png"] forState:UIControlStateSelected]; 
     [button setBackgroundImage:[UIImage imageNamed:@"bar_button_black.png"] forState:UIControlStateHighlighted]; 

    } 
} 
+0

UINavigationButton是UINavigationBar的一個私有子類,它不可訪問。 – ytur

相關問題