2015-05-20 76 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) 

这正常工作对我!

相关问题