【发布时间】:2018-05-24 18:47:13
【问题描述】:
我已经搜索了一段时间,但我只找到了描述在一个视图控制器上更改颜色而不是所有视图控制器的答案。
有可能吗?
【问题讨论】:
标签: ios swift uiviewcontroller statusbar uicolor
我已经搜索了一段时间,但我只找到了描述在一个视图控制器上更改颜色而不是所有视图控制器的答案。
有可能吗?
【问题讨论】:
标签: ios swift uiviewcontroller statusbar uicolor
在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 中将基于视图控制器的状态栏外观设置为 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
}
}
结果如下:
这是Apple Guidelines/Instruction关于状态栏更改的内容。状态栏中只允许使用深色和浅色(白色和黑色)。
这里是 - 如何更改状态栏样式:
如果你想设置状态栏样式,应用程序级别然后在你的`.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 的值。
【讨论】:
斯威夫特 4 在 AppDelegate.swift 添加这个扩展:
extension UINavigationController {
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
【讨论】:
是的, 第1步: 打开你的 info.plist 并插入一个名为“查看基于控制器的状态栏外观”的新键到 NO
第 2 步:
打开你要修改statusBarStyle的viewcontroller文件,在viewWillAppear()中输入如下代码,
UIApplication.shared.statusBarStyle = .lightContent
第 3 步:
另外,为特定的 viewController 实现 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
到是
这样,每当打开一个新的视图控制器时,如果您将此实现插入到您需要的每个 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
}
为什么要破解?如果您需要黑色或白色以外的状态栏颜色。在这里,您使用未记录的 API。您使用 KVC 获取 statusBar 对象并手动设置其颜色。这是肮脏的,不合法的方式,但到目前为止,这是为状态栏设置自定义颜色的唯一方法。这很可能会导致您的应用被拒绝。但也许你很幸运。为了一劳永逸地设置它,您需要将上述标志设置为 NO,以便状态栏不会使用每个视图控制器初始化其样式。
【讨论】: