[英]iOS Custom Status Bar Background Color not displaying
我正在尝试使用以下内容将状态栏背景颜色填充为橙色
UINavigationBar.appearance().tintColor = UIColor.orangeColor()
UINavigationBar.appearance().barTintColor = UIColor.orangeColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
但是,我得到一个白色的状态栏应该用橙色填充而不是遵循这个例子: 使用swift自定义导航栏外观
我在didFinishLaunchingWithOptions方法下的AppDelegate.swift文件中进行设置,以将其应用于整个应用程序。
我已将info.plist编辑为以下内容: View controller-based status bar appearance => NO
有谁知道我做错了什么?
编辑:我不确定它是否重要,但视图是在UITabBarController中
编辑2:这实际上发生在所有视图中,而不仅仅是UITabBarController 。
编辑3:谢谢@Utsav Parikh
我现在在状态栏的顶部添加一个视图,当应用程序加载状态栏时,它只是橙色但是,一旦完成加载,它就会被推到视图之外,并被替换为通用的白色状态栏。 为什么会这样?
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)
没有嵌入式控制器
我意识到有些人不仅来到状态栏,而且实际上是导航栏,所以我在学习过程中学到了一些技巧,没有任何嵌入式控制器:
在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)
没有嵌入式控制器
我意识到有些人不仅来到状态栏,而且实际上是导航栏,所以我在学习过程中学到了一些技巧,没有任何嵌入式控制器:
在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,我将以下子视图添加到我的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中不能很好地运行。 如果有人有更好的方式让我知道,但是,截至目前这是我已经解决的解决方案。
在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)
希望它可以帮助你.... !!!
这是我在没有在NavBarController中添加VC中的视图的情况下完成的
我希望状态栏的颜色与VC视图颜色相同,所以我写道:
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.grayColor()
self.navigationController?.navigationBar.clipsToBounds = true
}
试试吧。
我认为您的最后一行是还原您的更改,请尝试以下操作:
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()]
}
在info.plist
你做了以下操作之后: View controller-based status bar appearance
=>否。
在didFinishLaunchingWithOptions
下的AppDelegate.swift
文件中添加此代码:
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
你可以选择任何十六进制代码供你选择颜色.. !! 请享用..!!
对不起,忘了使用hexcode,你也需要这个,所以在你的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)
}
西蒙的答案很快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
}
tintColor
和更改UINavigationBar
的背景颜色有一个主要区别。 在我看来,最好的方法是应用背景图像,由1个像素的正方形图像制成,只有一种颜色。
像那样:
let tabbarAndNavBarBkg = UIImage(named: "nav_tab")
UINavigationBar.appearance().setBackgroundImage(tabbarAndNavBarBkg, forBarMetrics: .Default)
或者您可以在UIColor
上创建一个类别,以在objC中返回给定UIColor
实例的UIImage
:
+ (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;
}
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()
斯威夫特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)
这对我来说很好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.