Pat*_*rra 208 uinavigationbar storyboard uikit ios ios7
如何在iOS 7中更改导航栏颜色?
基本上我想要实现像Twitter Nav Bar这样的东西(更新的Twitter iOS7就是这样).我嵌入了一个导航栏view controller.我想要的是将导航栏颜色更改为浅蓝色以及顶部的实用工具栏.我似乎找不到我的选择storyboard.
Raj*_*071 316
tintColor在iOS 7.0中,for栏的行为已发生变化.它不再影响酒吧的背景.
从文档:
barTintColor 类参考
应用于导航栏背景的色调颜色.
@property(nonatomic, retain) UIColor *barTintColor
Run Code Online (Sandbox Code Playgroud)
讨论
默认情况下,此颜色为半透明,除非您将半透明属性设置为NO.
可用性
适用于iOS 7.0及更高版本.
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];
}
Run Code Online (Sandbox Code Playgroud)
在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;
}
Run Code Online (Sandbox Code Playgroud)
编辑 使用xib

alo*_*ono 126
通过在Xcode中使用Interface Builder,可以很容易地完成原始问题 - 获取旧Twitter的Nav Bar外观,蓝色背景和白色文本.
这应该会得到你想要的.这是一个屏幕截图,可以更容易地看到进行更改的位置.

请注意,仅更改条形色调不会更改导航栏或状态栏中的文本颜色.风格也需要改变.
pNr*_*Nre 73
self.navigationBar.barTintColor = [UIColor blueColor];
self.navigationBar.tintColor = [UIColor whiteColor];
self.navigationBar.translucent = NO;
// *barTintColor* sets the background color
// *tintColor* sets the buttons color
Run Code Online (Sandbox Code Playgroud)
RFG*_*RFG 44
在基于导航的应用程序中,您可以将代码放在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];
Run Code Online (Sandbox Code Playgroud)
小智 23
在viewDidLoad,设置:
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
Run Code Online (Sandbox Code Playgroud)
将(blueColor)更改为您想要的任何颜色.
Wil*_* Hu 16
对于快速更改导航栏颜色:
self.navigationController?.navigationBar.barTintColor = UIColor.red
Run Code Online (Sandbox Code Playgroud)
更改标题字体,大小,颜色:
self.title = "title"
self.navigationController?.navigationBar.titleTextAttributes = [
NSAttributedString.Key.foregroundColor : UIColor.white,
NSAttributedString.Key.font : UIFont(name: "Futura", size: 30)!
]
Run Code Online (Sandbox Code Playgroud)
RFG*_*RFG 12
在iOS 7中,您必须使用-barTintColor属性:
navController.navigationBar.barTintColor = [UIColor barColor];
Run Code Online (Sandbox Code Playgroud)
Kyl*_*law 12
如果您想使用十六进制代码,这是最好的方法.
首先,在您的班级顶部定义:
#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]
Run Code Online (Sandbox Code Playgroud)
然后在"应用程序didFinishLaunchingWithOptions"中,放入:
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x00b0f0)];
Run Code Online (Sandbox Code Playgroud)
用十六进制代码代替00b0f0.
bba*_*iee 11
如果您需要支持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];
}
}
Run Code Online (Sandbox Code Playgroud)
它实际上比我在这里看到的答案更容易:
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).
Run Code Online (Sandbox Code Playgroud)
我希望这有助于某人.我不喜欢我看到的答案.我喜欢尽可能保持我的代码清洁.并不是说以编程方式执行它是错误的,但有些人像我一样......这是给你们的.

小智 5
//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;
}
Run Code Online (Sandbox Code Playgroud)
为了使 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];
}
Run Code Online (Sandbox Code Playgroud)
仅在您的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]];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
282370 次 |
| 最近记录: |