繁体   English   中英

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

[英]IOS 5 How to change the color of back button in a navigation bar?

我想更改导航栏的后退按钮的颜色,使其看起来像这样 在此处输入图片说明

设置backBarButtonItemtintColor

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM