iOS 自定义状态栏背景颜色不显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30341224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS Custom Status Bar Background Color not displaying
提问by Simon
I am trying to fill the status bar background color to orange using the following
我正在尝试使用以下方法将状态栏背景颜色填充为橙色
UINavigationBar.appearance().tintColor = UIColor.orangeColor()
UINavigationBar.appearance().barTintColor = UIColor.orangeColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
However, I get a white status bar that should be filled with orange instead from following this example: Customize navigation bar appearance with swift
但是,我得到了一个白色的状态栏,应该用橙色填充,而不是按照这个例子:Customize navigation bar appearance with swift
I am setting this up in the AppDelegate.swiftfile under didFinishLaunchingWithOptionsmethod to apply it to the entire app.
我在AppDelegate.swift文件中的didFinishLaunchingWithOptions方法下进行了设置,以将其应用于整个应用程序。
I have edited my info.plistto the following: View controller-based status bar appearance => NO
我已将info.plist编辑为以下内容:View controller-based status bar appearance => NO
Does anyone know what I am doing wrong?
有谁知道我做错了什么?
Edit: I'm not sure if it matters but the view is in a UITabBarController
编辑:我不确定这是否重要,但视图在UITabBarController 中
Edit 2: This is happening in all the views actually, not just the UITabBarController.
编辑 2:这实际上发生在所有视图中,而不仅仅是UITabBarController。
Edit 3: Thanks @UtsavParikh
编辑 3:谢谢@UtsavParikh
I am adding a view now on top of the status bar and it for a brief moment while the app loads the status bar is orange but, once it finishes loading it gets pushed OFF the view and replaced with the generic white status bar. Why would this be happening?
我现在在状态栏顶部添加一个视图,当应用程序加载状态栏时它是橙色的,但是,一旦它完成加载,它就会被推离视图并替换为通用的白色状态栏。为什么会发生这种情况?
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)
Edit for Swift 3:
为Swift 3编辑:
with UITabBarController
使用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)
Without embedded controllers
无嵌入式控制器
I realize some people come here not only for the status bar, but actually the navigation bar, so I learned a few tricks along the way to do it without any embedded controllers:
我意识到有些人来到这里不仅是为了状态栏,而且实际上是为了导航栏,所以我在没有任何嵌入式控制器的情况下学习了一些技巧:
Add this method in your AppDelegate.swiftand call it in the didFinishLaunchingWithOptions
在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
}
回答by Simon
Edit for Swift 3:
为Swift 3编辑:
With UITabBarController
使用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)
Without embedded controllers
无嵌入式控制器
I realize some people come here not only for the status bar, but actually the navigation bar, so I learned a few tricks along the way to do it without any embedded controllers:
我意识到有些人来到这里不仅是为了状态栏,而且实际上是为了导航栏,所以我在没有任何嵌入式控制器的情况下学习了一些技巧:
Add this method in your AppDelegate.swiftand call it in the didFinishLaunchingWithOptions
在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
}
Thanks to @UtsavI added the following subview to my UITabBarControllerand this seems to be working now:
感谢@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)
The UITabBarControllerdoesn't seem to play well in AppDelegate. If anyone has a better way let me know but, as of now this is the solution I have come around to.
该的UITabBarController似乎并没有在玩好AppDelegate中。如果有人有更好的方法,请告诉我,但是,到目前为止,这是我想到的解决方案。
回答by Utsav Parikh
Add this code in didFinishLaunchingWithOptionsin AppDelegate
在AppDelegate 的 didFinishLaunchingWithOptions中添加此代码
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)
Hope it helps you....!!!
希望能帮到你....!!!
回答by naz
This is how I did it without adding a view in a VC with in a NavBarController
这就是我在没有在 NavBarController 的 VC 中添加视图的情况下做到的
I wanted the color of the status bar to be the same as the VC view color so I just wrote:
我希望状态栏的颜色与 VC 视图颜色相同,所以我只写了:
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.grayColor()
self.navigationController?.navigationBar.clipsToBounds = true
}
Try it.
尝试一下。
回答by Warif Akhand Rishi
Simon's answer in swift 3
西蒙在 swift 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)
There is one other way I know which uses private api. This has some benefits when orientation changes and keyboard is presented and view move up. I've used it and was lucky every time (app was released in the app store).
我知道还有另一种使用私有 api 的方式。当方向改变并且键盘出现并且视图向上移动时,这有一些好处。我使用过它并且每次都很幸运(应用程序已在应用程序商店中发布)。
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
statusBar.backgroundColor = color
}
回答by Icaro
I think your last line is reverting your changes, try this:
我认为您的最后一行正在恢复您的更改,试试这个:
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()]
}
回答by Irshad Qureshi
After what u did in info.plist
to the following: View controller-based status bar appearance
=> NO.
在info.plist
您对以下内容做了什么之后:View controller-based status bar appearance
=> 否。
Add this code in AppDelegate.swift
file under didFinishLaunchingWithOptions
:
将此代码添加到以下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
and u can select any hex code for ur choice of color..!! Enjoy..!!
您可以为您选择的颜色选择任何十六进制代码..!!享受..!!
Sorry, forgot to use hexcode you will be needing this also so add this code anywhere in your AppDelegate.swift
:
抱歉,忘记使用十六进制代码,您也将需要它,因此在您的任何位置添加此代码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)
}
回答by Andrea
There is a main difference in tintColor
and changing the background color of UINavigationBar
. The best way in my opinion is apply a background image, made by 1 pixel square image of just one color.
Like that:
有一个主要区别tintColor
和改变背景颜色UINavigationBar
。我认为最好的方法是应用背景图像,由仅一种颜色的 1 像素方形图像制成。
像那样:
let tabbarAndNavBarBkg = UIImage(named: "nav_tab")
UINavigationBar.appearance().setBackgroundImage(tabbarAndNavBarBkg, forBarMetrics: .Default)
Or you can create a category on UIColor
to return a UIImage
given a UIColor
instance, in objC:
或者您可以在 objC 中创建一个类别UIColor
以返回UIImage
给定的UIColor
实例:
+ (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;
}
回答by zisoft
UINavigationBar.appereance()
works for upcoming viewControllers, but not the currently displayed rootViewController. To achieve this I have added the following to my didFinishLaunchingWithOptions
:
UINavigationBar.appereance()
适用于即将到来的视图控制器,但不适用于当前显示的 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()
回答by Bruno Campos
Swift 3:
斯威夫特 3:
In your AppDelegate.swift file paste the code bellow into your didFinishLaunchingWithOptionsmethod:
在您的 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)
This works fine for me!
这对我来说很好用!