Swift-修改状态栏颜色
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift-修改状态栏颜色相关的知识,希望对你有一定的参考价值。
参考技术A 使用UIApplication.shared.statusBarStyle = .darkContent设置状态栏颜色,Xcode提示: 'statusBarStyle' was deprecated in ios 13.0: Use the statusBarManager property of the window scene instead.在baseController中重写 preferredStatusBarStyle
如果你的页面有导航栏,那么使用这这种方式是无法生效的。此时需要在自定义的NavigationController中加入重写childViewControllerForStatusBarStyle方法
Xcode 13 - swift OS 15 中的导航栏和状态栏文本颜色变化
【中文标题】Xcode 13 - swift OS 15 中的导航栏和状态栏文本颜色变化【英文标题】:Xcode 13 - Navigation bar and status bar text color changing in swift OS 15 【发布时间】:2022-01-10 10:15:10 【问题描述】:最近我将我的 Xcode 更新为 13,之后,我遇到了导航栏和状态栏的一些问题。我在我的视图控制器中使用标签栏。更新Xcode后,根据版本,添加了一些导航栏相关的代码。
if #available(iOS 15.0, *)
tableView.sectionHeaderTopPadding = 0
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor(red: 58/255,green: 24/255, blue: 93/255, alpha: 1.0)
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
// Customizing our navigation bar
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.barTintColor = .white
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
当我第一次打开应用程序时,一切正常。当我单击另一个选项卡然后单击此选项卡时。状态栏文本颜色正在改变。
我尝试了不同的方法来设置状态栏文本颜色。但没有什么对我有用。
【问题讨论】:
【参考方案1】:使用此功能:
extension YourViewController
override var preferredStatusBarStyle: UIStatusBarStyle
.lightContent
【讨论】:
【参考方案2】:如果您只想将整个项目中的状态栏设置为白色,您可以通过将以下键添加到 project.plist 文件中轻松实现:
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
那么,这些键有什么作用...您只是定义状态栏颜色将不受任何视图控制器控制,并且默认颜色将是“浅色主题”,几乎是白色在这种情况下你想要...
如果您不打算到处更新状态栏颜色,这将是最好的方法。
这已经过测试,并且在 Xcode 13 和 iOS [13.x,14.x,15.x] 上按预期工作
如果您想更新以自定义或更新导航栏的颜色,您应该能够使用如下所示的扩展程序,仅举几个例子,但你们应该明白我的意思:
extension UINavigationBar
func clearBackground()
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithTransparentBackground()
navBarAppearance.shadowColor = UIColor.clear
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
self.standardAppearance = navBarAppearance
self.scrollEdgeAppearance = navBarAppearance
func blackColor()
// Create an Appearance and customise as you wish.
// in this case just a black background with titles in white.
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.shadowColor = UIColor.black
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
self.standardAppearance = navBarAppearance
self.scrollEdgeAppearance = navBarAppearance
使用此代码,您应该能够通过调用以下代码轻松更新任何视图控制器:
override func viewDidLoad()
super.viewDidLoad()
// set the navigation bar color as transparent
self.navigationController?.navigationBar.clearBackground()
【讨论】:
以上是关于Swift-修改状态栏颜色的主要内容,如果未能解决你的问题,请参考以下文章