iOS 7 UIBarButton 返回按钮箭头颜色

173

我正在尝试更改后退按钮箭头。

enter image description here

我目前使用以下代码来控制后退按钮的文本大小和颜色:

[[UIBarButtonItem appearance] setTitleTextAttributes:
  [NSDictionary dictionaryWithObjectsAndKeys:
    [UIColor whiteColor], UITextAttributeTextColor,
    [UIFont boldSystemFontOfSize:16.0f], UITextAttributeFont,
    [UIColor darkGrayColor], UITextAttributeTextShadowColor,
    [NSValue valueWithCGSize:CGSizeMake(0.0, -1.0)], UITextAttributeTextShadowOffset,
  nil] forState:UIControlStateNormal];

但如果我只想为返回按钮更改箭头的颜色,该怎么办?


1
你找到了改变返回按钮箭头颜色的解决方案吗? - OnkarK
1
@OMK 我最终改变了我的infolistproperty NavBarColor来使它工作,然后将实际的navbarcolor设置为不同的颜色。我不确定发生了什么,但这个解决方案对我起作用了。 - kevinl
1
UINavigationBar 的一些属性行为已经从 iOS 7 发生了改变。查看这个答案以了解其他属性的影响。 - Bhavin
请帮忙解决这个问题:http://stackoverflow.com/questions/29923813/ios-back-button-arrow-not-displayed - kresa
17个回答

5

我必须同时使用以下两种方法:

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] 
                     setTitleTextAttributes:[NSDictionary 
               dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] 
                                   forState:UIControlStateNormal];

[[self.navigationController.navigationBar.subviews lastObject] setTintColor:[UIColor whiteColor]];

对我来说很有效,谢谢大家!


3

如果你正在基于UIButton创建带有箭头图像的自定义返回按钮,这里有一个子类片段。使用它,你可以在代码中创建按钮或者只是在Interface Builder中分配类到任何UIButton上。 后退箭头图像将自动添加并根据文本颜色着色。

@interface UIImage (TintColor)

- (UIImage *)imageWithOverlayColor:(UIColor *)color;

@end


@implementation UIImage (TintColor)

- (UIImage *)imageWithOverlayColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, self.size.width, self.size.height);

    if (UIGraphicsBeginImageContextWithOptions) {
        CGFloat imageScale = 1.0f;
        if ([self respondsToSelector:@selector(scale)])
            imageScale = self.scale;
        UIGraphicsBeginImageContextWithOptions(self.size, NO, imageScale);
    }
    else {
        UIGraphicsBeginImageContext(self.size);
    }

    [self drawInRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(context, kCGBlendModeSourceIn);

    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

@end




#import "iOS7backButton.h"

@implementation iOS7BackButton

-(void)awakeFromNib
{
    [super awakeFromNib];

    BOOL is6=([[[UIDevice currentDevice] systemVersion] floatValue] <7);
    UIImage *backBtnImage = [[UIImage imageNamed:@"backArrow"] imageWithOverlayColor:self.titleLabel.textColor];
    [self setImage:backBtnImage forState:UIControlStateNormal];
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
    [self setImageEdgeInsets:UIEdgeInsetsMake(0, is6?0:-10, 0, 0)];


}


+ (UIButton*) buttonWithTitle:(NSString*)btnTitle andTintColor:(UIColor*)color {
    BOOL is6=([[[UIDevice currentDevice] systemVersion] floatValue] <7);
    UIButton *backBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    UIImage *backBtnImage = [[UIImage imageNamed:@"backArrow"] imageWithOverlayColor:color];
    [backBtn setImage:backBtnImage forState:UIControlStateNormal];
    [backBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, is6?5:-5, 0, 0)];
    [backBtn setImageEdgeInsets:UIEdgeInsetsMake(0, is6?0:-10, 0, 0)];
    [backBtn setTitle:btnTitle forState:UIControlStateNormal];
    [backBtn setTitleColor:color /*#007aff*/ forState:UIControlStateNormal];

    return backBtn;
}

@end

back button image@2x


3

只需更改NavigationBar的颜色,您可以像下面这样设置色调颜色。

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

这不会影响iOS 7中的色调颜色。根据文档: “在iOS 7中,不支持使用外观代理API设置tintColor属性。” [链接](https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/TransitionGuide/AppearanceCustomization.html) - bachonk

3
如果你只想改变整个应用程序的返回箭头,而不仅仅是一个页面上的箭头,请按照以下步骤操作:
[[NSClassFromString(@"_UINavigationBarBackIndicatorView") appearance] 
  setTintColor:[UIColor colorWithHexString: @"#f00000"]];

这可能会导致您的应用被应用商店拒绝。 - Luke
@Luke,我已经成功使用过几次了。没有被拒绝的情况。但是,是的,仍然存在机会。 - orkenstein
未来的iOS版本仍然有可能会出现故障。 - Lope
1
@Lope,当然可以。我不是在寻找万能药。 - orkenstein
我查看了UINavigationBar的所有子视图以及这些子视图的所有子视图(_UINavigationBarBackIndicatorView没有子视图),但没有发现任何有用的东西。顺便说一句,我尝试直接将tintColor添加到_UINavigationBarBackIndicatorView和其他子视图中。那样做有点效果,但在导航时颜色会闪烁间断。 - arlomedia
显示剩余2条评论

2

在iOS 7中,您可以将以下代码行放置在AppDelegate.m文件中的application:didFinishLaunchingWithOptions:中:

[[UINavigationBar appearance] setTintColor:myColor];

myColor设置为您希望整个应用程序中返回按钮的颜色。无需在每个文件中都设置。


0

Swift 2.0: 给导航栏和按钮上色

navigationController?.navigationBar.barTintColor = UIColor.blueColor()
navigationController?.navigationBar.tintColor = UIColor.whiteColor()
navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

0
在Swift 3中,更改UIBarButton返回按钮箭头颜色
self.navigationController?.navigationBar.tintColor = UIColor.black

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接