IOS 5如何更改导航栏中后退按钮的颜色?

时间:2011-10-28 12:40:41

标签: ios cocoa-touch uinavigationbar back-button

我想更改导航栏后退按钮的颜色,使其看起来像enter image description here

5 个答案:

答案 0 :(得分:63)

设置backBarButtonItem的{​​{1}}:

tintColor

提示:如果您希望默认情况下将其应用于应用中的所有 self.navigationItem.backBarButtonItem.tintColor = [UIColor redColor]; 实例,则可以使用新的{{3} API就是这样做的:

UIBarButtonItem

答案 1 :(得分:8)

jacob的第一行答案对我不起作用,因为backBarButtonItem为NULL。它为NULL,因为它是在切换到其他ViewController时自动创建的。那时你可以用

设置按钮的标题
self.title = @"nice title"; // self is here the view(controller) within the popoverController

但您无法设置tintColor。

对我有用的是创建一个没有任何风格的新UIBarButtonItem。然后设置标题和颜色属性并将其设置为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];

答案 2 :(得分:3)

如果您想使按钮看起来与图片完全相同,您也可以使用图像:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"back_button_bg"]
                                        forState:UIControlStateNormal
                                      barMetrics:UIBarMetricsDefault];

背景图片必须是可调整大小的图像才能获得良好效果。

答案 3 :(得分:0)

我发现在全球或本地设置它的最佳方式是

    [[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];

答案 4 :(得分:-1)

[[UINavigationBar appearance]setTintColor:[UIColor whiteColor]];

试试这对我有用......