我已经搜索了一段时间,但只找到了描述如何在一个视图控制器上更改颜色的答案,并没有针对所有视图控制器的方法。
这是否有可能实现?
我已经搜索了一段时间,但只找到了描述如何在一个视图控制器上更改颜色的答案,并没有针对所有视图控制器的方法。
这是否有可能实现?
在 AppDelegate.swift
中设置状态栏的样式:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarStyle = .lightContent
return true
}
将以下代码添加到您的 Info.plist
中:
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
首先在info.plist中将“View controller-based status bar appearance”设置为NO
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
statusBar.backgroundColor = UIColor.blue
}
UIApplication.shared.statusBarStyle = .lightContent
return true
}
您可以在应用启动期间或在视图控制器的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
}
}
这是结果:
这里是关于状态栏变化的苹果指南/说明。只有深色和浅色(白色和黑色)可以在状态栏中使用。
以下是- 如何更改状态栏样式:
如果您想要设置应用程序级别的状态栏样式,请在`.plist'文件中将UIViewControllerBasedStatusBarAppearance
设置为NO
。
或者您可以从应用程序委托中以编程方式进行更改:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.statusBarStyle = .lightContent
return true
}
如果你想在视图控制器级别上设置状态栏样式,请按照以下步骤进行:
.plist
文件中将UIViewControllerBasedStatusBarAppearance
设置为YES
。在viewDidLoad
函数中添加setNeedsStatusBarAppearanceUpdate
函数。
重写您的视图控制器中的preferredStatusBarStyle
函数。
-
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
根据状态栏样式设置级别来设置.plist的值。
Swift 4
extension UINavigationController {
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
是的, 步骤1: 打开您的info.plist文件并插入一个名为“View controller-based status bar appearance”的新键,将其设置为NO
步骤2:
打开您想要更改statusBarStyle的viewcontroller
文件,并在viewWillAppear()
中放入以下代码:
UIApplication.shared.statusBarStyle = .lightContent
步骤三:
此外,为该特定视图控制器实现viewWillDisappear()
方法,并添加以下代码行:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
}
祝一切顺利
了解自定义状态栏的两种方法非常重要。
是否可以为所有视图控制器更改状态栏颜色?
布尔答案是是,但是,在合法的方式下,如果您需要除黑色或白色以外的颜色,则回答“是”是具有挑衅性的。
当涉及到自定义状态栏外观时,有两种方法。
在info.plist中,您可以找到或创建一个名为
View controller-based status bar appearance
并将其设置为NO。
它是做什么的?它实际上建立了一个设置,该设置表示在您的应用程序中,状态栏外观不是由每个视图控制器单独定义的。这非常重要。这意味着您拥有整个应用程序的统一设置,适用于所有屏幕。这就是您所需要的。但是。您只能使用两个设置:白色和黑色。而且没有办法使用文档化的API进行自定义。
要设置此项:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.statusBarStyle = .lightContent
return true
}
这相反于第一种方法。要使其起作用,请前往 info.plist 并设置
View controller-based status bar appearance
为YES
这样,每当打开新的视图控制器时,如果您在每个需要的 UIViewController
实例中插入此实现,则状态栏风格会被单独设置:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
你可以像第一种方法一样,为状态栏设置深色或浅色风格。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let statusbar = value(forKey: "statusBar") as? UIView {
statusbar.backgroundColor = UIColor.blue
}
return true
}