[英]How to change Navigation Bar color in iOS 7?
如何在iOS 7中更改導航欄顏色?
基本上我想要實現類似Twitter Nav Bar(更新的iOS7
推特)。 我嵌入了一個view controller
頂部的導航欄。 我想要的是將導航欄顏色更改為淺藍色以及頂部的實用工具欄。 我似乎無法在storyboard
找到一個選項。
在iOS 7.0中,條形的tintColor
的行為已更改。 它不再影響酒吧的背景。
從文檔:
barTintColor 類參考
應用於導航欄背景的色調顏色。
@property(nonatomic, retain) UIColor *barTintColor
討論
默認情況下,此顏色為半透明,除非您將半透明屬性設置為NO
。
可用性
適用於iOS 7.0及更高版本。
宣告進入
UINavigationBar.h
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];
}
在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;
}
編輯使用xib
通過在Xcode中使用Interface Builder,可以很容易地完成原始問題 - 獲取舊Twitter的Nav Bar外觀,藍色背景和白色文本。
這應該會得到你想要的。 這是一個屏幕截圖,可以更容易地看到進行更改的位置。
請注意,僅更改條形色調不會更改導航欄或狀態欄中的文本顏色。 風格也需要改變。
self.navigationBar.barTintColor = [UIColor blueColor];
self.navigationBar.tintColor = [UIColor whiteColor];
self.navigationBar.translucent = NO;
// *barTintColor* sets the background color
// *tintColor* sets the buttons color
在基於導航的應用程序中,您可以將代碼放在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];
在viewDidLoad
,設置:
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
將( blueColor
)更改為您想要的任何顏色。
對於快速更改導航欄顏色:
self.navigationController?.navigationBar.barTintColor = UIColor.red
更改標題字體,大小,顏色:
self.title = "title"
self.navigationController?.navigationBar.titleTextAttributes = [
NSAttributedString.Key.foregroundColor : UIColor.white,
NSAttributedString.Key.font : UIFont(name: "Futura", size: 30)!
]
如果您想使用十六進制代碼,這是最好的方法。
首先,在您的班級頂部定義:
#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]
然后在“應用程序didFinishLaunchingWithOptions”中,放入:
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x00b0f0)];
用十六進制代碼代替00b0f0。
在iOS 7中,您必須使用-barTintColor屬性:
navController.navigationBar.barTintColor = [UIColor barColor];
如果您需要支持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];
}
}
它實際上比我在這里看到的答案更容易:
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).
我希望這有助於某人。 我不喜歡我看到的答案。 我喜歡盡可能保持我的代碼清潔。 並不是說以編程方式執行它是錯誤的,但有些人像我一樣......這是給你們的。
要使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];
}
僅在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]];
}
//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;
}
在基於導航的應用程序中,您可以更改顏色
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];
self.navigationController.navigationBar.translucent = NO;
} else {
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];
}
#define _kisiOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
if (_kisiOS7)
{
[[UINavigationBar appearance] setBarTintColor:[UIcolor redcolor]];
}
else
{
[[UINavigationBar appearance] setBackgroundColor:[UIcolor blackcolor]];
[[UINavigationBar appearance] setTintColor:[UIcolor graycolor]];
}
這個問題和這些答案都很有幫助。 通過它們,我可以設置我想要的深藍色navigationBar
顏色,白色標題和按鈕文字。
但我還需要將時鍾,載波,信號強度等改為白色。 黑色與深藍色的對比度不夠。
我可能在前面的一個答案中忽略了這個解決方案,但我能夠通過將此行添加到我的頂級viewController
的viewDidLoad
來進行更改:
[self.navigationController.navigationBar setBarStyle:UIStatusBarStyleLightContent];
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
}
對於顏色:
[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
對於圖像
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar_320X44.png"] forBarMetrics:UIBarMetricsDefault];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.