iOS自定义状态栏背景颜色不显示 [英] iOS Custom Status Bar Background Color not displaying
问题描述
我正在尝试使用以下内容将状态栏背景颜色填充为橙色
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
我在 didFinishLaunchingWithOptions 方法下的 AppDelegate.swift 文件中进行设置将它应用于整个应用程序。
I am setting this up in the AppDelegate.swift file under didFinishLaunchingWithOptions method to apply it to the entire app.
我已将 info.plist 编辑为以下内容: 查看基于控制器的状态栏外观= >否
I have edited my info.plist to the following: View controller-based status bar appearance => NO
有谁知道我做错了什么?
Does anyone know what I am doing wrong?
编辑:我不确定它是否重要,但视图位于 UITabBarController
I'm not sure if it matters but the view is in a UITabBarController
编辑2:这在所有视图中都会发生实际上,不仅仅是 UITabBarController 。
Edit 2: This is happening in all the views actually, not just the UITabBarController.
编辑3:谢谢 @Utsav Parikh
Edit 3: Thanks @Utsav Parikh
我现在在状态栏的顶部添加一个视图,当应用程序加载状态栏时它只是橙色但是,一旦完成加载,它就会被推到视图之外并被替换为通用白色状态栏。
为什么会发生这种情况?
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)
编辑 Swift 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)
没有嵌入式控制器
我意识到有些人不仅仅是为了状态栏,而且实际上是导航栏,所以我在学习过程中学到了一些技巧,没有任何嵌入式控制器:
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:
在 AppDelegate.swift 中添加此方法并在
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
}
推荐答案
编辑 Swift 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)
没有嵌入式控制器
我意识到有些人来到这里不仅是状态栏,而且实际上是导航栏,所以我学习了一些技巧,没有任何嵌入式控制器:
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:
在你的方法中添加此方法 AppDelegate.swift 并在 didFinishLaunchingWithOptions
Add this method in your AppDelegate.swift and call it in the 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
}
感谢 @Utsav 我添加了以下子视图我的 UITabBarController ,现在似乎正在运作:
Thanks to @Utsav I added the following subview to my UITabBarController and this seems to be working now:
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 中发挥出色。如果有人有更好的方式让我知道,但是,截至目前这是我已经解决的解决方案。
The UITabBarController doesn'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.
这篇关于iOS自定义状态栏背景颜色不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!