我正在尝试更改后退按钮箭头
我目前正在使用以下内容来控制文本大小以及后退按钮上的文本颜色:
[[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];
但如果我只想更改后退按钮的箭头颜色,我该怎么办?
答案 0 :(得分:438)
更改特定导航控制器的后退按钮V形颜色*:
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
*如果您使用的应用程序具有多个导航控制器,并且您希望将此V形颜色应用于每个导航控制器,则可能需要使用外观代理为每个导航控制器设置后退按钮V形符号,如下所示:
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
好的方面,快速(感谢Jay Mayu在评论中):
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
答案 1 :(得分:57)
您必须设置整个应用的tintColor。
self.window.tintColor = [UIColor redColor];
或者在Swift 3中:
self.window?.tintColor = UIColor.blue
答案 2 :(得分:55)
您可以使用
方法在整个应用导航条上设置颜色- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions{
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
}
答案 3 :(得分:23)
可以通过这种方式改变箭头的颜色(不是后退按钮标题的颜色):
[[self.navigationController.navigationBar.subviews lastObject] setTintColor:[UIColor blackColor]];
导航栏包含_UINavigationBarBackIndicatorView类型的子视图(子视图数组中的最后一项),表示箭头。
结果为navigation bar with different colors of back button arrow and back button title
答案 4 :(得分:22)
答案 5 :(得分:11)
在初始化navigationController的rootViewController中,我将此代码放在viewDidAppear方法中:
//set back button color
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
//set back button arrow color
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
答案 6 :(得分:7)
在iOS 6中,tintColor着色了导航栏,标签栏,工具栏,搜索栏和范围栏的背景。要在iOS 7中为条形背景着色,请改用barTintColor属性。
答案 7 :(得分:6)
您可以在按钮(或条形按钮项)或视图控制器视图上设置tintColor
属性。默认情况下,该属性将从父视图继承色调,一直到应用程序的顶级UIWindow
。
答案 8 :(得分:5)
我必须同时使用两者:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setTitleTextAttributes:[NSDictionary
dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil]
forState:UIControlStateNormal];
[[self.navigationController.navigationBar.subviews lastObject] setTintColor:[UIColor whiteColor]];
对我有用,谢谢大家!
答案 9 :(得分:5)
UINavigationBar *nbar = self.navigationController.navigationBar;
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
//iOS 7
nbar.barTintColor = [UIColor blueColor]; // bar color
//or custom color
//[UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];
nbar.navigationBar.translucent = NO;
nbar.tintColor = [UIColor blueColor]; //bar button item color
} else {
//ios 4,5,6
nbar.tintColor = [UIColor whiteColor];
//or custom color
//[UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];
}
答案 10 :(得分: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
答案 11 :(得分:3)
如果您只想更改整个应用程序的后退箭头,请执行以下操作:
[[NSClassFromString(@"_UINavigationBarBackIndicatorView") appearance]
setTintColor:[UIColor colorWithHexString: @"#f00000"]];
答案 12 :(得分:3)
只需更改NavigationBar
颜色即可设置色彩颜色,如下所示。
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
答案 13 :(得分:3)
更新Swift 3
navigationController?.navigationItem.rightBarButtonItem?.tintColor = UIColor.yellow
navigationController?.navigationBar.tintColor = UIColor.red
navigationController?.navigationBar.barTintColor = UIColor.gray
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blue]
答案 14 :(得分:2)
在iOS 7中,您可以将以下代码行放在application:didFinishLaunchingWithOptions:
文件中的AppDelegate.m
内:
[[UINavigationBar appearance] setTintColor:myColor];
将myColor
设置为您希望后退按钮在整个应用中的颜色。无需将其放在每个文件中。
答案 15 :(得分:0)
Swift 2.0:着色导航栏&amp;按钮
navigationController?.navigationBar.barTintColor = UIColor.blueColor()
navigationController?.navigationBar.tintColor = UIColor.whiteColor()
navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
答案 16 :(得分:0)
在swift 3中,更改UIBarButton后退按钮箭头颜色
self.navigationController?.navigationBar.tintColor = UIColor.black