我如何改变导航栏的颜色在iOS 7?
基本上我想要实现类似Twitter导航栏的东西(更新的Twitter为iOS7)。我在视图控制器的顶部嵌入了一个导航条。所有我想要的是改变导航栏的颜色为浅蓝色连同顶部的工具栏。我似乎在我的故事板中找不到一个选项。
我如何改变导航栏的颜色在iOS 7?
基本上我想要实现类似Twitter导航栏的东西(更新的Twitter为iOS7)。我在视图控制器的顶部嵌入了一个导航条。所有我想要的是改变导航栏的颜色为浅蓝色连同顶部的工具栏。我似乎在我的故事板中找不到一个选项。
当前回答
在ios7中,你必须使用-barTintColor属性:
navController.navigationBar.barTintColor = [UIColor barColor];
其他回答
做最初的问题——得到旧的Twitter的导航栏外观,蓝色背景和白色文本——很容易做到,只需要使用Xcode中的界面生成器。
Using the Document Outline, select your Navigation Bar. In the Attributes Inspector, in the Navigation Bar group, change the Style from Default to Black. This changes the background colour of the Navigation and Status bars to black, and their text to white. So the battery and other icons and text in the status bar will look white when the app is running. In the same Navigation Bar group, change the Bar Tint to the colour of your liking. If you have Bar Button items in your Navigation Bar, those will still show their text in the default blue colour, so in the Attributes Inspector, View group, change the Tint to White Colour.
这应该能让你得到你想要的。下面是一个截图,可以让你更容易地看到在哪里进行更改。
注意,只改变条形颜色不会改变导航栏或状态栏的文本颜色。样式也需要改变。
self.navigationBar.barTintColor = [UIColor blueColor];
self.navigationBar.tintColor = [UIColor whiteColor];
self.navigationBar.translucent = NO;
// *barTintColor* sets the background color
// *tintColor* sets the button's color
在viewcontroller或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]];
}
在一个基于导航的应用程序中,你可以把代码放在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];
这个问题和这些答案很有帮助。有了它们,我可以设置我想要的深蓝色导航栏颜色,白色标题和按钮文本。
但我还需要将时钟、载波、信号强度等更改为白色。黑色和深蓝色的对比不够明显。
我可能在之前的回答中忽略了这个解决方案,但我能够通过在我的顶级viewController的viewDidLoad中添加这一行来进行更改:
[self.navigationController.navigationBar setBarStyle:UIStatusBarStyleLightContent];