[英]Set toolbar color and navigationbar back button
我想在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"]];
但是沒有顯示背景圖像。
我想知道這是做什么的:
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"]];
}
Nd如何更改默認導航欄的后退按鈕顏色:
在iOS中,你必須使用7 toolbar.barTintColor
設置彩色, toolbar.tintColor
將設置工具欄里面的barButtons的顏色。
最后一段代碼測試該應用程序是否在iOS 7上運行,如果使用的是barTintColor
,否則,將使用tintColor
。
要更改backButton,您可以通過設置navigationBar的tintColor
來執行相同操作
此代碼基本上是確定iOS是什么,並選擇正確的方法調用來設置navigationBar的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)
iOS 7 if ([[ver objectAtIndex:0] intValue] >= 7)
也會返回FALSE。
要設置工具欄,您可以使用相同的方法,但替換為:
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"]]];
//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
}
}
您可以使用以下代碼...
[toolBar setBackgroundImage:toolBarIMG forToolbarPosition:0 barMetrics:0];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.