[英]How to change the background color of navigationbar in ios6 and ios7 both?
[英]How to change the background color of NavigationBar in iOS 7
我是Objective-C的新手,今天我嘗試更改導航欄的顏色,並且此代碼適用於此代碼:
appDelegate.m :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0xf4f4f4)];
return YES;
}
現在,我嘗試更改特定的ViewController導航欄,但這不起作用。
ViewController2.m:
- (void)viewDidLoad
{
[super viewDidLoad];
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x363636)];
self.navigationController.navigationBar.translucent = NO;
}
當我輸入此ViewContoller時,其導航顏色為f4f4f4
,當我轉到其他ViewController並返回時,其顏色更改為363636
。
為什么這在第一時間不起作用? 有人可以向我解釋一下嗎。
(對不起,我的英語,謝謝。)
當你使用
[UINavigationBar appearance]
它會更改所有導航欄。 在您的視圖控制器中,只需執行
[self.navigationBar setBarTintColor:UIColorFromRGB(0x363636)];
您可以在視圖控制器的-(void) viewDidLoad
方法中實現Nathanael所說的內容。 但是,如果您使用情節提要板,則還可以選擇navigation item
並從“ File inspector
更改顏色,而無需編寫代碼。
使用導航欄的tintcolor屬性更改顏色
您可以嘗試一下,我在AppDelegate中更改了導航欄顏色,因此所有應用程序都具有相同的顏色,但是我必須在單個視圖控制器中更改導航欄顏色,所以我做到了:
在AppDelegate中
let color = UIColor(red:0.24, green:0.72, blue:0.28, alpha:1.0)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().barTintColor = color
在viewDidLoad的視圖控制器中,我有這個:
self.navigationController?.navigationBar.barTintColor = UIColor.blueColor()
在viewWillDissappear
如下:
super.viewWillDisappear(animated)
let color = UIColor(red:0.24, green:0.72, blue:0.28, alpha:1.0)
self.navigationController?.navigationBar.barTintColor = color
它將導航欄顏色恢復為相同的顏色
對於iOS 10,您可以使用如下所示的barStyle:
對於白色條形:
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
對於黑條顏色:
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
需要在ViewDidLoad中設置BarStyle才能進行常規動畫工作:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.