Iphone 无法在iOS导航栏中设置颜色
我正在尝试为iPhone应用程序中的导航栏设置不同的颜色。我的应用程序在iOS 7中运行,下面是我使用的代码片段Iphone 无法在iOS导航栏中设置颜色,iphone,objective-c,ios7,uinavigationbar,Iphone,Objective C,Ios7,Uinavigationbar,我正在尝试为iPhone应用程序中的导航栏设置不同的颜色。我的应用程序在iOS 7中运行,下面是我使用的代码片段 NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."]; if ([[ver objectAtIndex:0] intValue] >= 7) { self.navigationController.navigationBar.barTintColo
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.navigationController.navigationBar.translucent = NO;
}else {
self.navigationController.navigationBar.tintColor = [UIColor redColor];
}
但是上面的代码并没有将导航栏的颜色更改为红色。我错过了什么吗。你能帮帮我吗。谢谢将以下代码放入您的AppDelegate中的didFinishLaunchingWithOptions中
if ([self checkOSVersion] >= 7) {
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setTranslucent:NO];
} else {
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
}
- (int)checkOSVersion {
NSArray *ver = [[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."];
int osVerson = [[ver objectAtIndex:0] intValue];
return osVerson;
}
将以下代码放入您的AppDelegate中的didFinishLaunchingWithOptions中
if ([self checkOSVersion] >= 7) {
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setTranslucent:NO];
} else {
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
}
- (int)checkOSVersion {
NSArray *ver = [[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."];
int osVerson = [[ver objectAtIndex:0] intValue];
return osVerson;
}
用于为appdelegate.m中代码下面的所有导航栏默认设置添加颜色
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
return YES;
}
或者要为特定视图设置颜色,请使用此选项
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.barTintColor = [UIColor grayColor];
}
如果您想了解更多信息,请尝试此链接,为appdelegate.m中代码下面的所有导航栏默认设置添加颜色是很好的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
return YES;
}
或者要为特定视图设置颜色,请使用此选项
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.barTintColor = [UIColor grayColor];
}
如果您想了解更多信息,请尝试此链接,它很好如果([[ver objectAtIndex:0]intValue]>=7),我会说If语句失败。我将用以下语句替换整个
if语句
:
if([[[UIDevice currentDevice] systemVersion] intValue] >= 7) {
// Do the below if you want to set the bar tint color for every UINavigationBar
// [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
// [[UINavigationBar appearance] setTranslucent:NO];
[[[self navigationController] navigationBar] setBarTintColor:[UIColor redColor]];
[[[self navigationController] navigationBar] setTranslucent:NO];
} else {
// Do the below if you want to set the bar tint color for every UINavigationBar
// [[UINavigationBar appearance] setTintColor:[UIColor redColor]];
[[[self navigationController] navigationBar] setTintColor:[UIColor redColor]];
}
[[UIDevice currentDevice]系统版本]
将返回一个NSString*
,该字符串可以是7
到7.0.1
之间的任意值。执行floatValue
操作将至少返回7.0
,因为如果它只返回7
,它将在末尾附加0
,因此我们希望它始终只给我们7
的初始系统版本,所以我们需要在末尾执行intValue
,这样看起来像[[UIDevice currentDevice]systemVersion]intValue]
忘记组件分离的dbysting:
方法根本不需要它,它会导致比您想要的更多的问题
如果语句
if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_7_0) {
// Everything above and including iOS 7.0
} else {
// Everything below iOS 7.0
}
如果([[ver objectAtIndex:0]intValue]>=7),我会说它没有通过if语句。我将用以下语句替换整个
if语句
:
if([[[UIDevice currentDevice] systemVersion] intValue] >= 7) {
// Do the below if you want to set the bar tint color for every UINavigationBar
// [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
// [[UINavigationBar appearance] setTranslucent:NO];
[[[self navigationController] navigationBar] setBarTintColor:[UIColor redColor]];
[[[self navigationController] navigationBar] setTranslucent:NO];
} else {
// Do the below if you want to set the bar tint color for every UINavigationBar
// [[UINavigationBar appearance] setTintColor:[UIColor redColor]];
[[[self navigationController] navigationBar] setTintColor:[UIColor redColor]];
}
[[UIDevice currentDevice]系统版本]
将返回一个NSString*
,该字符串可以是7
到7.0.1
之间的任意值。执行floatValue
操作将至少返回7.0
,因为如果它只返回7
,它将在末尾附加0
,因此我们希望它始终只给我们7
的初始系统版本,所以我们需要在末尾执行intValue
,这样看起来像[[UIDevice currentDevice]systemVersion]intValue]
忘记组件分离的dbysting:
方法根本不需要它,它会导致比您想要的更多的问题
如果语句
if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_7_0) {
// Everything above and including iOS 7.0
} else {
// Everything below iOS 7.0
}
添加代码委托文件或??确保您的
self.navigationController
不应为零。如果([[[UIDevice currentDevice]systemVersion]floatValue]>=7,请尝试)
改为使用array.where添加代码委托文件或??确保您的self.navigationController
不应为零。如果([[[UIDevice currentDevice]systemVersion]floatValue]>=7),请尝试使用。