2015-05-20 143 views
15

我嘗試使用以下iOS的自定義狀態欄背景顏色顯示不

UINavigationBar.appearance().tintColor = UIColor.orangeColor() 
UINavigationBar.appearance().barTintColor = UIColor.orangeColor() 
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()] 
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true) 

不過,我得到的是應該從以下充滿橙色,而不是白色狀態欄填滿狀態欄的背景顏色爲橙色下面這個例子:Customize navigation bar appearance with swift

我在AppDelegate.swift下的文件didFinishLaunchingWithOptions設置此功能方法將它應用到整個應用程序。

我已經編輯我的的info.plist以下幾點:View controller-based status bar appearance => NO

有誰知道我做錯了嗎?

編輯:我不知道,如果它的事項,但認爲是在的UITabBarController

編輯2:這是在所有的意見發生實際上,不只是的UITabBarController

編輯3:謝謝@Utsav帕瑞克

我添加視圖現在狀態欄的頂部,它短暫的瞬間,而應用程序加載狀態欄是橙色的,但是,一旦它完成加載它被關閉視圖並被替換爲通用白色狀態欄。 爲什麼會發生這種情況?

let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0)) 
view.backgroundColor=UIColor.orangeColor() 
self.window!.rootViewController!.view.addSubview(view) 

編輯爲夫特3

的UITabBarController

let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0)) 
view.backgroundColor = .orange 
self.view.addSubview(view) 

沒有嵌入式控制器

我知道有些人來這裏不僅是狀態欄,但實際上在導航欄上,讓我學到了一些技巧,一起做沒有任何嵌入式控制器的方式:

添加這個方法在你AppDelegate.swift並調用它在didFinishLaunchingWithOptions

func customizeAppearance() { 
    UINavigationBar.appearance().barTintColor = UIColor.black 
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] 
    UITabBar.appearance().barTintColor = UIColor.black 
    let tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0) 

    UITabBar.appearance().tintColor = tintColor 
} 
+0

訪問這個鏈接...它可能有助於 [這裏] [1] [1]:http://stackoverflow.com/questions/26956728/changing-the-status-bar-color- for-specific-viewcontrollers-using-swift-in-ios8 –

+0

@AshokLondhe我在使用該鏈接之前詢問了這個問題,但沒有運氣。 – Simon

+0

更改常規 - >目標 - >狀態欄樣式下的狀態欄樣式。看看這是否有任何效果。也可以在應用程序委託啓動後通過調度嘗試更改外觀,然後等待2秒 – TheCodingArt

回答

26

編輯爲斯威夫特3

隨着的UITabBarController

let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0)) 
view.backgroundColor = .orange 
self.view.addSubview(view) 

沒有嵌入式控制器

我認識一些人來這裏不僅是狀態欄,但實際上在導航欄上,讓我學會沿途一些小技巧來做到這一點沒有任何嵌入式控制器:

AppDelegate.swift添加該方法並調用它在didFinishLaunchingWithOptions

func customizeAppearance() { 
    UINavigationBar.appearance().barTintColor = UIColor.black 
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] 
    UITabBar.appearance().barTintColor = UIColor.black 
    let tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0) 

    UITabBar.appearance().tintColor = tintColor 
} 

比KS到@Utsav添加以下子視圖我的UITabBarController,這似乎是現在的工作:

let view = UIView(frame: 
        CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0) 
       ) 
    view.backgroundColor = UIColor.orangeColor() 

    self.view.addSubview(view) 

的UITabBarController似乎並不的AppDelegate發揮出色。如果任何人有更好的方式讓我知道,但到目前爲止,這是我已經解決的解決方案。

+0

請upvote如果我的答案幫助你 –

+0

如果我添加此代碼像指示,我得到錯誤'不能調用非函數類型'UIScreen''的價值,我使用swift 3和xcode 8。有什麼建議嗎?我已經開始在這個問題上的賞金,所以如果你能找出這個問題,你會得到50個代表 –

+0

@EmmetArries [檢查這個答案](http://stackoverflow.com/a/41328197/1378447)​​ –

16

didFinishLaunchingWithOptions添加該代碼在AppDelegate中

let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0)) 
view.backgroundColor=UIColor.orangeColor() 
self.window.rootViewController.view.addSubview(view) 

希望它可以幫助你.... !!!

+0

@Simon在swift中檢查我更新的答案 –

+1

「UIScreen沒有名爲bounds的成員」是我目前正在獲取的錯誤,當我嘗試這個答案@Utsav – Simon

+0

@Simon嘗試UIScreen.mainScreen()。bounds.size。寬度,而不是UIScreen.mainScreen.bounds.size.width –

1

我覺得你的最後一行恢復您的更改,試試這個:

override func viewWillAppear(animated: Bool) { 
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true) 
     super.viewWillAppear(animated) 
     var nav = self.navigationController?.navigationBar 
     nav?.barStyle = UIBarStyle.Black 
     nav?.tintColor = UIColor.orangeColor() 
     nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()] 
    } 
+0

不幸的是,這產生了相同的結果。感謝您的幫助 – Simon

0

在有一個tintColor主要區別是,改變的UINavigationBar背景顏色。我認爲最好的方法是應用一個背景圖像,由一個顏色的1像素方塊圖像製成。
就像是:

let tabbarAndNavBarBkg = UIImage(named: "nav_tab") 
UINavigationBar.appearance().setBackgroundImage(tabbarAndNavBarBkg, forBarMetrics: .Default) 

或者你可以創建UIColor類別返回一個UIImage給予UIColor例如,在objC:

+ (UIImage *) imageWithColor:(UIColor*) color { 
    CGRect rect = CGRectMake(0, 0, 1, 1); 
    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColorWithColor(context, color.CGColor); 
    CGContextFillRect(context, rect); 
    UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return colorImage; 
} 
+0

我會盡快回復,謝謝! – Simon

+0

我試過這個沒成功。我應該在頂部應用視圖嗎?我會怎麼做呢? – Simon

+0

如果您使用第一行寫入的純色創建圖像,它是否工作? – Andrea

0

UINavigationBar.appereance()作品即將到來的viewControllers,但不目前顯示的是rootViewController。要做到這一點我已經添加以下到我的didFinishLaunchingWithOptions

UINavigationBar.appearance().tintColor = myColor 

let navigationController = UIApplication.sharedApplication().windows[0].rootViewController as! UINavigationController 
navigationController.navigationBar.barTintColor = myColor 
navigationController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : myTextColor] 
navigationController.navigationBar.translucent = false 

navigationController.setNeedsStatusBarAppearanceUpdate() 
+0

不幸的是,這也不能解決我的問題。它實際上是通過初始的UIView來關聯到這個視圖(UITabBarController)。 – Simon

1

什麼ü沒有在info.plist以下後:View controller-based status bar appearance => NO。

AppDelegate.swift文件中添加以下代碼didFinishLaunchingWithOptions下:

var navigationBarAppearace = UINavigationBar.appearance() 

navigationBarAppearace.tintColor = uicolorFromHex(0xffffff) 
navigationBarAppearace.barTintColor = uicolorFromHex(0x2E9AFE) 

// change navigation item title color 
navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()] 

UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent 

和u可以選擇烏爾選擇任何顏色的十六進制代碼.. !!請享用..!!

對不起,忘了使用十六進制編碼,你也將需要這也使您的AppDelegate.swift任何地方添加以下代碼:

func uicolorFromHex(rgbValue:UInt32)->UIColor { 

    let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0 

    let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0 

    let blue = CGFloat(rgbValue & 0xFF)/256.0 

    return UIColor(red:red, green:green, blue:blue, alpha:1.0) 
} 
+0

感謝分享,但是這並不能解決我目前的問題。它實際上看起來與我的相似。 – Simon

+0

嘿你有解決方案嗎? –

6

這是我做到了沒有一個NavBarController

將在VC視圖與我想要的狀態欄的顏色是一樣的VC視圖顏色,所以我只是寫:

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.view.backgroundColor = UIColor.grayColor() 
    self.navigationController?.navigationBar.clipsToBounds = true 
} 

試試吧。

+0

謝謝clipsToBounds –

1

西蒙在迅速3回答

let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0)) 
view.backgroundColor = .orange 
self.view.addSubview(view) 

還有另外一個辦法,我知道它使用私有API。當方向改變和鍵盤顯示並且視圖向上移動時,這具有一些優點。我使用過它,每次都很幸運(應用程序在應用程序商店中發佈)。

func setStatusBarBackgroundColor(color: UIColor) { 

    guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return } 

    statusBar.backgroundColor = color 
} 
+0

你是什麼意思的「私人API」?我如何設置第二個選項的顏色? 「幸運」是什麼意思?應用商店通常不會接受這種做事方式嗎?對不起,我有很多問題,但我是新來的迅速。 –

+0

順便說一句我沒有UITabBarController,所以第一個答案不適用於我。 –

0

斯威夫特3:

在你AppDelegate.swift文件波紋管將代碼粘貼到您的didFinishLaunchingWithOptions方法:

let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0)) 
view.backgroundColor = UIColor(red: 255/255, green: 130/255, blue: 0/255, alpha: 1.0) // Organge colour in RGB 
self.window?.rootViewController?.view.addSubview(view) 

這正常工作對我!

相關問題