2013-12-12 200 views
1

我想设置工具栏的背景色iOS7: 我设置的颜色与此:设置工具栏的颜色和导航栏后退按钮

toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,40)]; 
[toolBar setBarStyle:UIBarStyleBlack]; 
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,flexible,barButtonOther,nil]; 
toolBar.tintColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"dropdownbar"]]; 

但背景图像不显示。

enter image description here

而且我想知道这是什么一样:

NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."]; 
if ([[ver objectAtIndex:0] intValue] >= 7) { 
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]]; 
    self.navigationController.navigationBar.translucent = NO; 
} else { 
    self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]]; 

} 

钕怎么改回默认的导航栏上的按钮颜色:

enter image description here

enter image description here

+0

工具栏或导航栏他们是两个完全不同的事情。导航栏在顶部通常位于底部。 – Popeye

+0

@Popeye我知道那..但我想知道那代码会做什么? – vivek

+0

我其实在谈论你的形象。如果这是在顶部,它应该是一个导航栏而不是一个工具栏。 – Popeye

回答

7

在iOS中7,你必须使用toolbar.barTintColor设置彩色,toolbar.tintColor将设置工具栏里面的barButtons的颜色。

最后一段代码测试应用程序是否在iOS 7上运行,如果使用的是barTintColor,如果不是,则会使用tintColor

要改变后退按钮,你可以通过设定的导航栏

+0

我做了这个,然后我做了,其他按钮消失了; – vivek

+0

我使用'tintColor'和'barTintColor'工具栏上的颜色相同,您的按钮和工具栏将具有相同的颜色。 – KIDdAe

+0

不,不,你是对的...它的工作...错误我已经删除线添加按钮到工具栏... :) – vivek

0

你c下面的代码使用...

[toolBar setBackgroundImage:toolBarIMG forToolbarPosition:0 barMetrics:0]; 
1

此代码基本上确定了iOS是什么tintColor,并选择正确的方法调用来设置的导航栏的tintColor这样做。请参阅下面的代码中的评论

/* This is basically getting the systemVersion of the device so 7.0.1 - as this is returned 
    as an NSString the user is separating this string based on the string "." which will 
    place each string into the array. so you will have 3 objects in this array of "7", "0", "1" 
*/ 
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."]; 
/* Once we have separated this string into the array we are getting the first object in that array 
    so index `0` which will return a string at which point we are converting the string "7" to 
    an intValue and comparing it to the int 7. So if that value is 7 or greater return TRUE 
*/ 
if ([[ver objectAtIndex:0] intValue] >= 7) { 
    /* The value was TRUE so lets use the iOS 7 way of setting the tintColor. 
     We do this by setting barTintColor for the navigationBar and because 
     in iOS 7 the navigationBar can be translucent - by default this is YES 
     but because we are setting to a color we want we need to set this to NO 
     because we don't want to have it translucent. 
    */ 
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]]; 
    self.navigationController.navigationBar.translucent = NO; 
} else { 
    /* 
    If all fails in the if statements conditional check we must be on something 
    that is below iOS 7 so use the old way of things and just set the tintColor 
    */ 
    self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]]; 
} 

只是让你知道这是行不通的。如果你要使用它这个变化:

if (floor(NSFoundationVersionNumber) => NSFoundationVersionNumber_iOS_7_0) { 
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]]; 
    self.navigationController.navigationBar.translucent = NO; 
} else { 
    self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]]; 
} 

这是因为if ([[ver objectAtIndex:0] intValue] >= 7)将返回FALSE即使iOS的7

要设置工具栏,你可以用同样的方法,但替换:

self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]]; 
self.navigationController.navigationBar.translucent = NO; 

[toolbar setBarTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"dropdownbar"]]]; 
[toolbar setTranslucent:NO]; 

和替换

self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_bar"]]; 

[toolbar setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"dropdownbar"]]]; 
+0

很好...如果我只针对我的应用程序ios7然后没有必要检查操作系统版本正确...直接与bartintcolor? ? – vivek

+0

如果你只支持iOS 7+并且你的部署目标是iOS 7,那么这是正确的,那么就不需要'if语句',因为else部分永远不会达到一百万年。所以你可以设置barTintColor: – Popeye

1
//How to set color of *all* navigationbars and toolbars in an app in Swift 2.0 

    @UIApplicationMain 
    class AppDelegate: UIResponder, UIApplicationDelegate { 

     func application(application: UIApplication, didFinishLaunchingWithOptionslaunchOptions: [NSObject: AnyObject]?) -> Bool { 
     UINavigationBar.appearance().tintColor = UIColor.greenColor(); // All back button in the app are now green. 
     UIToolbar.appearance().tintColor = UIColor.orangeColor(); // All toolBar icons in the app are now orange. 

     //........ 
     let returnCode: Bool = getAppSpecificReturnCode(); 



     return returnCode 
    } 


    }