[英]IOS 5 How to change the color of back button in a navigation bar?
我想更改導航欄的后退按鈕的顏色,使其看起來像這樣
設置backBarButtonItem
的tintColor
:
self.navigationItem.backBarButtonItem.tintColor = [UIColor redColor];
提示 :如果您希望默認情況下將此方法應用於應用程序中的所有 UIBarButtonItem
實例,則可以使用新的UIAppearance
API來做到這一點:
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
雅各布回答的第一行對我不起作用,因為backBarButtonItem為NULL。 它為NULL,因為它是稍后切換到另一個ViewController時自動創建的。 那時您可以使用
self.title = @"nice title"; // self is here the view(controller) within the popoverController
但您無法設置tintColor。
對我有用的是創建一個沒有任何樣式的新UIBarButtonItem。 然后設置title和color屬性,並將其設置為backBarButtonItem。
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];
backButton.title = @"go back - now!";
backButton.tintColor = [UIColor colorWithRed:0.1 green:0.5 blue:0.7 alpha:1.0];
self.navigationItem.backBarButtonItem = backButton;
[okButton release];
如果您想使按鈕看起來與圖片完全一樣,則也可以使用圖像:
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"back_button_bg"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
為了獲得良好的效果,背景圖像必須是可調整大小的圖像。
我發現在全球或本地進行設置的最好方法是
[[UIBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0], UITextAttributeTextColor,
[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"AmericanTypewriter" size:0.0], UITextAttributeFont,
nil]
forState:UIControlStateNormal];
[[UINavigationBar appearance]setTintColor:[UIColor whiteColor]];
試試這個它對我有用...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.