在 Swift 3 中更改状态栏背景颜色
Change Status Bar Background Color in Swift 3
在 XCode 7.3.x 我更改了状态栏的背景颜色:
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
return
}
statusBar.backgroundColor = color
}
但似乎这对 Swift 3.0 不再有效。
我会尝试:
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = (UIApplication.shared.value(forKey: "statusBarWindow") as AnyObject).value(forKey: "statusBar") as? UIView else {
return
}
statusBar.backgroundColor = color
}
但它给了我:
this class is not key value coding-compliant for the key statusBar.
任何关于如何使用 XCode8/Swift 3.0 更改它的想法?
extension UIApplication {
var statusBarView: UIView? {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
return nil
}
}
UIApplication.shared.statusBarView?.backgroundColor = .red
更新 iOS 13
App called -statusBar or -statusBarWindow on UIApplication: this code
must be changed as there's no longer a status bar or status bar
window. Use the statusBarManager object on the window scene instead.
参考How to change the status bar background color and text color on iOS 13?
试试这个
转到您的应用程序 info.plist
- 将基于视图控制器的状态栏外观设置为否
- 将状态栏样式设置为 UIStatusBarStyleLightContent
然后转到您的应用委托并将以下代码粘贴到您设置 Windows 的 RootViewController 的位置。
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
view.backgroundColor=[UIColor blackColor];
[self.window.rootViewController.view addSubview:view];
}
"Change"状态栏背景颜色:
let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
let statusBarColor = UIColor(red: 32/255, green: 149/255, blue: 215/255, alpha: 1.0)
statusBarView.backgroundColor = statusBarColor
view.addSubview(statusBarView)
更改状态栏文本颜色:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
更新:请注意,旋转视图时状态栏框架会发生变化。您可以通过以下方式更新创建的子视图框架:
- 使用自动调整大小蒙版:
statusBarView.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
- 正在观察
NSNotification.Name.UIApplicationWillChangeStatusBarOrientation
- 或覆盖
viewWillLayoutSubviews()
对于 Xcode 9 和 iOS 11:
我们要尝试实现的状态栏样式是白色内容的状态栏。转到 ViewController.swift 文件并添加以下代码行。
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
或者您可以从项目设置选项更改状态栏的样式:
接下来,返回故事板,Select 视图控制器,然后在编辑器菜单中 Select 嵌入导航控制器。 Select 导航栏并在属性检查器中将栏色调颜色设置为红色。故事板将如下所示。
构建并运行工程,状态栏内容再次变暗,这是默认的。原因是,iOS 要求导航控制器状态栏的样式,而不是包含的视图控制器。
要更改应用内所有导航控制器的样式,请在 AppDelegate.swift 文件中更改以下方法。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().barStyle = .blackOpaque
return true
}
再次构建并运行 Project,这次状态栏的内容变为白色。
对于 iOS 11 和 Xcode 9 使用以下步骤。
创建 UIApplication 的扩展 class:
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
在您的 class 或您想要更改状态栏背景颜色的任何位置:
UIApplication.shared.statusBarView?.backgroundColor = .red
对于状态栏的浅色内容或深色内容,只需转到 Info.plist 并添加以下值为 的值行没有.
View controller-based status bar appearance
- 现在只需在项目设置的常规选项卡中设置灯光内容或任何您需要的内容。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any] ?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().barStyle = .blackOpaque
return true
}
这对我有用,因为我的导航栏 TintColor 是黑色的,看不到状态栏。
当设置上面的代码时,它 didFinishLaunch 状态栏显示为白色。
您可以在应用程序启动期间或视图控制器的 viewDidLoad 期间设置状态栏的背景颜色。
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}
or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}
}
这是结果:
通过使用 Swift 3 和 4,您可以使用下面的代码片段。它使用 valueForKeyPath
从 UIApplication
中找到视图,并设置它的背景颜色。
guard let statusBarView = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else {
return
}
statusBarView.backgroundColor = UIColor.red
Objective-C
UIView *statusBarView = [UIApplication.sharedApplication valueForKeyPath:@"statusBarWindow.statusBar"];
if (statusBarView != nil)
{
statusBarView.backgroundColor = [UIColor redColor];
}
可能的解决方案是在 viewController:
上添加将用作状态栏背景的视图
let frame = screenController.view.convert(UIApplication.shared.statusBarFrame, to: screenController.view)
let backview = UIView(frame: frame)
backview.backgroundColor = backgroundColor
screenController.view.addSubview(backview)
screenController.view.bringSubviewToFront(backview)
在您的扩展文件中添加以下代码以在 swift 4 及更高版本中编辑您的状态栏:
extension UIApplication {
var statusBarView: UIView? {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
return nil
}
}
现在,我们可以通过在 ViewController class 中添加以下行来编辑状态栏:
UIApplication.shared.statusBarView?.backgroundColor = <Your Color name>
ex: UIApplication.shared.statusBarView?.backgroundColor = .red
希望这会有所帮助。
谢谢
我做了这个扩展来改变状态栏的颜色。它不依赖于密钥。
所以使用
更安全
public extension UIViewController {
func setStatusBar(color: UIColor) {
let tag = 12321
if let taggedView = self.view.viewWithTag(tag){
taggedView.removeFromSuperview()
}
let overView = UIView()
overView.frame = UIApplication.shared.statusBarFrame
overView.backgroundColor = color
overView.tag = tag
self.view.addSubview(overView)
}
}
这里是 viewcontroller 中任何地方的用法:
setStatusBar(color: .red)
我有一个自定义解决方案,用于在 iOS 13 及以下更改状态栏。方法如下:
if #available(iOS 13.0, *) {
let app = UIApplication.shared
let statusBarHeight: CGFloat = app.statusBarFrame.size.height
let statusbarView = UIView()
statusbarView.backgroundColor = UIColor.red
view.addSubview(statusbarView)
statusbarView.translatesAutoresizingMaskIntoConstraints = false
statusbarView.heightAnchor
.constraint(equalToConstant: statusBarHeight).isActive = true
statusbarView.widthAnchor
.constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
statusbarView.topAnchor
.constraint(equalTo: view.topAnchor).isActive = true
statusbarView.centerXAnchor
.constraint(equalTo: view.centerXAnchor).isActive = true
} else {
let statusBar = UIApplication.shared.value(forKeyPath:
"statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = UIColor.red
}
Gist
另外,查看文章 iOS 13 How to Change StatusBar Color?
最后一件事,您仍然可以更改状态栏样式:
override var preferredStatusBarStyle : UIStatusBarStyle {
return UIStatusBarStyle.lightContent
//return UIStatusBarStyle.default // Make dark again
}
在第一个视图控制器中写入:
UIApplication.shared.statusBarUIView?.backgroundColor = .white
extension UIApplication {
var statusBarUIView: UIView? {
if #available(iOS 13.0, *) {
let tag = 38482458385
if let statusBar = self.keyWindow?.viewWithTag(tag) {
return statusBar
} else {
let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
statusBarView.tag = tag
self.keyWindow?.addSubview(statusBarView)
return statusBarView
}
} else {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
}
return nil
}
}
前一个代码:-
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
statusBar.backgroundColor = color
}
我的应用程序出现崩溃显示原因:“应用程序在 UIApplication 上调用了 -statusBar 或 -statusBarWindow:必须更改此代码,因为不再有状态栏或状态栏 window。请改用 window 场景中的 statusBarManager 对象。'
更新代码-
if #available(iOS 13.0, *) {
let statusBar = UIView(frame: UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
statusBar.backgroundColor = UIColor.init(red: 237.0/255.0, green: 85.0/255.0, blue: 61.0/255.0, alpha: 1.0)
UIApplication.shared.keyWindow?.addSubview(statusBar)
} else {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.init(red: 237.0/255.0, green: 85.0/255.0, blue: 61.0/255.0, alpha: 1.0)
}
此代码有效 swift 5.2
enter image description here
IOS15,Xcode13.2.1,Swift5
我能够使用以下方法使它在没有错误或警告的情况下工作:
func statusBarColor() {
if #available(iOS 13.0, *) {
let statusBar2 = UIView()
if UIApplication.shared.currentScene?.statusBarManager!.statusBarFrame != nil {
statusBar2.frame = (UIApplication.shared.currentScene?.statusBarManager!.statusBarFrame)!
statusBar2.backgroundColor = UIColor.init(named: "BackGroundColor")
UIApplication.shared.windows.first?.addSubview(statusBar2)
}
} else {
let statusBar2: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
statusBar2.backgroundColor = UIColor.init(named: "BackGroundColor")
}
}
用途:调用viewDidLoad中的函数用于单场景应用或者只需要改变一个statusBar颜色的应用。对于具有多个调用不同状态栏颜色的场景的应用程序,我建议在 viewWillAppear 中调用该函数。
在 XCode 7.3.x 我更改了状态栏的背景颜色:
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
return
}
statusBar.backgroundColor = color
}
但似乎这对 Swift 3.0 不再有效。
我会尝试:
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = (UIApplication.shared.value(forKey: "statusBarWindow") as AnyObject).value(forKey: "statusBar") as? UIView else {
return
}
statusBar.backgroundColor = color
}
但它给了我:
this class is not key value coding-compliant for the key statusBar.
任何关于如何使用 XCode8/Swift 3.0 更改它的想法?
extension UIApplication {
var statusBarView: UIView? {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
return nil
}
}
UIApplication.shared.statusBarView?.backgroundColor = .red
更新 iOS 13
App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.
参考How to change the status bar background color and text color on iOS 13?
试试这个
转到您的应用程序 info.plist
- 将基于视图控制器的状态栏外观设置为否
- 将状态栏样式设置为 UIStatusBarStyleLightContent
然后转到您的应用委托并将以下代码粘贴到您设置 Windows 的 RootViewController 的位置。
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
view.backgroundColor=[UIColor blackColor];
[self.window.rootViewController.view addSubview:view];
}
"Change"状态栏背景颜色:
let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
let statusBarColor = UIColor(red: 32/255, green: 149/255, blue: 215/255, alpha: 1.0)
statusBarView.backgroundColor = statusBarColor
view.addSubview(statusBarView)
更改状态栏文本颜色:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
更新:请注意,旋转视图时状态栏框架会发生变化。您可以通过以下方式更新创建的子视图框架:
- 使用自动调整大小蒙版:
statusBarView.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
- 正在观察
NSNotification.Name.UIApplicationWillChangeStatusBarOrientation
- 或覆盖
viewWillLayoutSubviews()
对于 Xcode 9 和 iOS 11: 我们要尝试实现的状态栏样式是白色内容的状态栏。转到 ViewController.swift 文件并添加以下代码行。
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
或者您可以从项目设置选项更改状态栏的样式:
接下来,返回故事板,Select 视图控制器,然后在编辑器菜单中 Select 嵌入导航控制器。 Select 导航栏并在属性检查器中将栏色调颜色设置为红色。故事板将如下所示。
构建并运行工程,状态栏内容再次变暗,这是默认的。原因是,iOS 要求导航控制器状态栏的样式,而不是包含的视图控制器。
要更改应用内所有导航控制器的样式,请在 AppDelegate.swift 文件中更改以下方法。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().barStyle = .blackOpaque
return true
}
再次构建并运行 Project,这次状态栏的内容变为白色。
对于 iOS 11 和 Xcode 9 使用以下步骤。
创建 UIApplication 的扩展 class:
extension UIApplication { var statusBarView: UIView? { return value(forKey: "statusBar") as? UIView } }
在您的 class 或您想要更改状态栏背景颜色的任何位置:
UIApplication.shared.statusBarView?.backgroundColor = .red
对于状态栏的浅色内容或深色内容,只需转到 Info.plist 并添加以下值为 的值行没有.
View controller-based status bar appearance
- 现在只需在项目设置的常规选项卡中设置灯光内容或任何您需要的内容。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any] ?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().barStyle = .blackOpaque
return true
}
这对我有用,因为我的导航栏 TintColor 是黑色的,看不到状态栏。
当设置上面的代码时,它 didFinishLaunch 状态栏显示为白色。
您可以在应用程序启动期间或视图控制器的 viewDidLoad 期间设置状态栏的背景颜色。
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}
or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}
}
这是结果:
通过使用 Swift 3 和 4,您可以使用下面的代码片段。它使用 valueForKeyPath
从 UIApplication
中找到视图,并设置它的背景颜色。
guard let statusBarView = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else {
return
}
statusBarView.backgroundColor = UIColor.red
Objective-C
UIView *statusBarView = [UIApplication.sharedApplication valueForKeyPath:@"statusBarWindow.statusBar"];
if (statusBarView != nil)
{
statusBarView.backgroundColor = [UIColor redColor];
}
可能的解决方案是在 viewController:
上添加将用作状态栏背景的视图let frame = screenController.view.convert(UIApplication.shared.statusBarFrame, to: screenController.view)
let backview = UIView(frame: frame)
backview.backgroundColor = backgroundColor
screenController.view.addSubview(backview)
screenController.view.bringSubviewToFront(backview)
在您的扩展文件中添加以下代码以在 swift 4 及更高版本中编辑您的状态栏:
extension UIApplication {
var statusBarView: UIView? {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
return nil
}
}
现在,我们可以通过在 ViewController class 中添加以下行来编辑状态栏:
UIApplication.shared.statusBarView?.backgroundColor = <Your Color name>
ex: UIApplication.shared.statusBarView?.backgroundColor = .red
希望这会有所帮助。 谢谢
我做了这个扩展来改变状态栏的颜色。它不依赖于密钥。 所以使用
更安全public extension UIViewController {
func setStatusBar(color: UIColor) {
let tag = 12321
if let taggedView = self.view.viewWithTag(tag){
taggedView.removeFromSuperview()
}
let overView = UIView()
overView.frame = UIApplication.shared.statusBarFrame
overView.backgroundColor = color
overView.tag = tag
self.view.addSubview(overView)
}
}
这里是 viewcontroller 中任何地方的用法:
setStatusBar(color: .red)
我有一个自定义解决方案,用于在 iOS 13 及以下更改状态栏。方法如下:
if #available(iOS 13.0, *) {
let app = UIApplication.shared
let statusBarHeight: CGFloat = app.statusBarFrame.size.height
let statusbarView = UIView()
statusbarView.backgroundColor = UIColor.red
view.addSubview(statusbarView)
statusbarView.translatesAutoresizingMaskIntoConstraints = false
statusbarView.heightAnchor
.constraint(equalToConstant: statusBarHeight).isActive = true
statusbarView.widthAnchor
.constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
statusbarView.topAnchor
.constraint(equalTo: view.topAnchor).isActive = true
statusbarView.centerXAnchor
.constraint(equalTo: view.centerXAnchor).isActive = true
} else {
let statusBar = UIApplication.shared.value(forKeyPath:
"statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = UIColor.red
}
Gist
另外,查看文章 iOS 13 How to Change StatusBar Color?
最后一件事,您仍然可以更改状态栏样式:
override var preferredStatusBarStyle : UIStatusBarStyle {
return UIStatusBarStyle.lightContent
//return UIStatusBarStyle.default // Make dark again
}
在第一个视图控制器中写入:
UIApplication.shared.statusBarUIView?.backgroundColor = .white
extension UIApplication {
var statusBarUIView: UIView? {
if #available(iOS 13.0, *) {
let tag = 38482458385
if let statusBar = self.keyWindow?.viewWithTag(tag) {
return statusBar
} else {
let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
statusBarView.tag = tag
self.keyWindow?.addSubview(statusBarView)
return statusBarView
}
} else {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
}
return nil
}
}
前一个代码:-
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
statusBar.backgroundColor = color
}
我的应用程序出现崩溃显示原因:“应用程序在 UIApplication 上调用了 -statusBar 或 -statusBarWindow:必须更改此代码,因为不再有状态栏或状态栏 window。请改用 window 场景中的 statusBarManager 对象。'
更新代码-
if #available(iOS 13.0, *) {
let statusBar = UIView(frame: UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
statusBar.backgroundColor = UIColor.init(red: 237.0/255.0, green: 85.0/255.0, blue: 61.0/255.0, alpha: 1.0)
UIApplication.shared.keyWindow?.addSubview(statusBar)
} else {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.init(red: 237.0/255.0, green: 85.0/255.0, blue: 61.0/255.0, alpha: 1.0)
}
此代码有效 swift 5.2
enter image description here
IOS15,Xcode13.2.1,Swift5
我能够使用以下方法使它在没有错误或警告的情况下工作:
func statusBarColor() {
if #available(iOS 13.0, *) {
let statusBar2 = UIView()
if UIApplication.shared.currentScene?.statusBarManager!.statusBarFrame != nil {
statusBar2.frame = (UIApplication.shared.currentScene?.statusBarManager!.statusBarFrame)!
statusBar2.backgroundColor = UIColor.init(named: "BackGroundColor")
UIApplication.shared.windows.first?.addSubview(statusBar2)
}
} else {
let statusBar2: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
statusBar2.backgroundColor = UIColor.init(named: "BackGroundColor")
}
}
用途:调用viewDidLoad中的函数用于单场景应用或者只需要改变一个statusBar颜色的应用。对于具有多个调用不同状态栏颜色的场景的应用程序,我建议在 viewWillAppear 中调用该函数。