iOS导航栏的标题颜色默认是白色。有没有办法换一种颜色?
我知道navigationItem。使用图像的titleView方法。由于我的设计技能有限,我没有得到标准的光泽,我更喜欢改变文字的颜色。
任何见解都将非常感激。
iOS导航栏的标题颜色默认是白色。有没有办法换一种颜色?
我知道navigationItem。使用图像的titleView方法。由于我的设计技能有限,我没有得到标准的光泽,我更喜欢改变文字的颜色。
任何见解都将非常感激。
当前回答
如果你试图改变一个页面的颜色,tewha的解决方案效果很好,但我希望能够改变每个页面的颜色。我做了一些小的修改,使它可以工作在UINavigationController上的所有页面
NavigationDelegate.h
//This will change the color of the navigation bar
#import <Foundation/Foundation.h>
@interface NavigationDelegate : NSObject<UINavigationControllerDelegate> {
}
@end
NavigationDelegate.m
#import "NavigationDelegate.h"
@implementation NavigationDelegate
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
CGRect frame = CGRectMake(0, 0, 200, 44);//TODO: Can we get the size of the text?
UILabel* label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor yellowColor];
//The two lines below are the only ones that have changed
label.text=viewController.title;
viewController.navigationItem.titleView = label;
}
@end
其他回答
这是我基于史蒂文斯的解决方案
唯一真正的区别是,如果根据文本长度,我将一些处理用于调整位置,似乎与苹果的做法类似
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(([self.title length] < 10 ? UITextAlignmentCenter : UITextAlignmentLeft), 0, 480,44)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont boldSystemFontOfSize: 20.0f];
titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleLabel.textAlignment = ([self.title length] < 10 ? UITextAlignmentCenter : UITextAlignmentLeft);
titleLabel.textColor = [UIColor redColor];
titleLabel.text = self.title;
self.navigationItem.titleView = titleLabel;
[titleLabel release];
您可能需要根据字体大小调整10值
这适用于我在Swift:
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
当我们在导航栏中插入一个按钮时,遇到了相同的问题(和其他人一样),标签移动(在我的情况下,我有一个旋转器,当日期被加载时,我用一个按钮替换),上述解决方案对我不起作用,所以这里是什么工作,并保持标签在同一位置一直:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// this will appear as the title in the navigation bar
//CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont boldSystemFontOfSize:20.0]].width, 44);
CGRect frame = CGRectMake(0, 0, 180, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor yellowColor];
self.navigationItem.titleView = label;
label.text = NSLocalizedString(@"Latest Questions", @"");
[label sizeToFit];
}
return self;
self.navigationItem.title=@"Extras";
[self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaNeue" size:21], NSFontAttributeName,[UIColor whiteColor],UITextAttributeTextColor,nil]];
我使用下面的代码为iOS 9和它的工作对我来说很好。我也使用阴影颜色的标题。
self.navigationItem.title = @"MY NAV TITLE";
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.navigationController.navigationBar.translucent = NO;
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
[self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
shadow, NSShadowAttributeName,
[UIFont fontWithName:@"HelveticaNeue-Light" size:21.0], NSFontAttributeName, nil]];
希望这对你有所帮助。
谢谢