[英]How to change Navigation Bar color in iOS 7?
如何在iOS 7中更改导航栏颜色?
基本上我想要实现类似Twitter Nav Bar(更新的iOS7
推特)。 我嵌入了一个view controller
顶部的导航栏。 我想要的是将导航栏颜色更改为浅蓝色以及顶部的实用工具栏。 我似乎无法在storyboard
找到一个选项。
在iOS 7.0中,条形的tintColor
的行为已更改。 它不再影响酒吧的背景。
从文档:
barTintColor 类参考
应用于导航栏背景的色调颜色。
@property(nonatomic, retain) UIColor *barTintColor
讨论
默认情况下,此颜色为半透明,除非您将半透明属性设置为NO
。
可用性
适用于iOS 7.0及更高版本。
宣告进入
UINavigationBar.h
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
// iOS 7.0 or later
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.navigationController.navigationBar.translucent = NO;
}else {
// iOS 6.1 or earlier
self.navigationController.navigationBar.tintColor = [UIColor redColor];
}
在iOS 7 UI Transition Guide中,我们也可以使用它来检查iOS版本
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// iOS 6.1 or earlier
self.navigationController.navigationBar.tintColor = [UIColor redColor];
} else {
// iOS 7.0 or later
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.navigationController.navigationBar.translucent = NO;
}
编辑使用xib
通过在Xcode中使用Interface Builder,可以很容易地完成原始问题 - 获取旧Twitter的Nav Bar外观,蓝色背景和白色文本。
这应该会得到你想要的。 这是一个屏幕截图,可以更容易地看到进行更改的位置。
请注意,仅更改条形色调不会更改导航栏或状态栏中的文本颜色。 风格也需要改变。
self.navigationBar.barTintColor = [UIColor blueColor];
self.navigationBar.tintColor = [UIColor whiteColor];
self.navigationBar.translucent = NO;
// *barTintColor* sets the background color
// *tintColor* sets the buttons color
在基于导航的应用程序中,您可以将代码放在AppDelegate中。 更详细的代码可能是:
// Navigation bar appearance (background and title)
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor titleColor], NSForegroundColorAttributeName, [UIFont fontWithName:@"FontNAme" size:titleSize], NSFontAttributeName, nil]];
[[UINavigationBar appearance] setTintColor:[UIColor barColor]];
// Navigation bar buttons appearance
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor textBarColor], NSForegroundColorAttributeName, shadowColor, NSShadowAttributeName, [UIFont fontWithName:@"FontName" size:titleSize], NSFontAttributeName, nil];
在viewDidLoad
,设置:
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
将( blueColor
)更改为您想要的任何颜色。
对于快速更改导航栏颜色:
self.navigationController?.navigationBar.barTintColor = UIColor.red
更改标题字体,大小,颜色:
self.title = "title"
self.navigationController?.navigationBar.titleTextAttributes = [
NSAttributedString.Key.foregroundColor : UIColor.white,
NSAttributedString.Key.font : UIFont(name: "Futura", size: 30)!
]
如果您想使用十六进制代码,这是最好的方法。
首先,在您的班级顶部定义:
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
然后在“应用程序didFinishLaunchingWithOptions”中,放入:
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x00b0f0)];
用十六进制代码代替00b0f0。
在iOS 7中,您必须使用-barTintColor属性:
navController.navigationBar.barTintColor = [UIColor barColor];
如果您需要支持ios6和ios7,那么在UIViewController中使用它可以获得特定的淡蓝色:
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:89/255.0f green:174/255.0f blue:235/255.0f alpha:1.0f];
self.navigationController.navigationBar.translucent = NO;
}else{
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:89/255.0f green:174/255.0f blue:235/255.0f alpha:1.0f];
}
}
它实际上比我在这里看到的答案更容易:
1) Just make sure you select the navigation bar on the Navigation control.
2) Select the color you want in the bar tint.
3) You have other options too, and/or individually on each view (just play with it).
我希望这有助于某人。 我不喜欢我看到的答案。 我喜欢尽可能保持我的代码清洁。 并不是说以编程方式执行它是错误的,但有些人像我一样......这是给你们的。
要使Rajneesh071的代码完整,您可能还需要设置导航栏的标题颜色(和字体,如果需要),因为默认行为从iOS 6更改为7:
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7)
{
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
self.navigationController.navigationBar.translucent = NO;
NSMutableDictionary *textAttributes = [[NSMutableDictionary alloc] initWithDictionary:mainNavController.navigationBar.titleTextAttributes];
[textAttributes setValue:[UIColor whiteColor] forKey:UITextAttributeTextColor];
self.navigationController.navigationBar.titleTextAttributes = textAttributes;
}
else
{
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
}
仅在ViewContorller
或 AppDelegate
添加此代码
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
{
//This is For iOS6
[self.navigationController.navigationBar setTintColor:[UIColor yellowColor]];
}
else
{
//This is For iOS7
[self.navigationController.navigationBar setBarTintColor:[UIColor yellowColor]];
}
//You could place this code into viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.tintColor = [UIColor redColor];
//change the nav bar colour
self.navigationController.view.backgroundColor = [UIColor redColor];
//change the background colour
self.navigationController.navigationBar.translucent = NO;
}
//Or you can place it into viewDidAppear
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:(BOOL)animated];
self.navigationController.navigationBar.tintColor = [UIColor redColor];
//change the nav bar colour
self.navigationController.view.backgroundColor = [UIColor redColor];
//change the background colour
self.navigationController.navigationBar.translucent = NO;
}
在基于导航的应用程序中,您可以更改颜色
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];
self.navigationController.navigationBar.translucent = NO;
} else {
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];
}
#define _kisiOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
if (_kisiOS7)
{
[[UINavigationBar appearance] setBarTintColor:[UIcolor redcolor]];
}
else
{
[[UINavigationBar appearance] setBackgroundColor:[UIcolor blackcolor]];
[[UINavigationBar appearance] setTintColor:[UIcolor graycolor]];
}
这个问题和这些答案都很有帮助。 通过它们,我可以设置我想要的深蓝色navigationBar
颜色,白色标题和按钮文字。
但我还需要将时钟,载波,信号强度等改为白色。 黑色与深蓝色的对比度不够。
我可能在前面的一个答案中忽略了这个解决方案,但我能够通过将此行添加到我的顶级viewController
的viewDidLoad
来进行更改:
[self.navigationController.navigationBar setBarStyle:UIStatusBarStyleLightContent];
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
}
对于颜色:
[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
对于图像
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar_320X44.png"] forBarMetrics:UIBarMetricsDefault];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.