我正在尝试使用以下方法将状态栏背景颜色填充为橙色
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
有人知道我做错了什么吗?
编辑:我不确定它是否重要,但 View 位于 UITabBarController
编辑 2:这实际上发生在所有 View 中,而不仅仅是 UITabBarController。
编辑 3:谢谢 @Utsav Parikh
我现在在状态栏顶部添加一个 View ,当应用程序加载状态栏时,它会短暂显示橙色,但是一旦加载完成,它就会被推离 View 并替换为通用的白色状态栏. 为什么会这样?
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)
没有嵌入式 Controller
我意识到有些人来这里不仅是为了状态栏,实际上也是为了导航栏,所以我在没有任何嵌入式 Controller 的情况下学习了一些技巧:
在您的 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
}
最佳答案
为 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)
没有嵌入式 Controller
我意识到有些人来这里不仅是为了状态栏,实际上也是为了导航栏,所以我在没有任何嵌入式 Controller 的情况下学习了一些技巧:
在您的 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
}
感谢 @Utsav 我将以下 subview 添加到我的 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 中表现不佳。如果有人有更好的方法让我知道,但到目前为止,这是我已经想到的解决方案。
关于iOS自定义状态栏背景颜色不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30341224/