ios15以后,发现之前的写法无法使状态栏改变颜色了,经过查资料解决了。废话不多说,以下是代码
解决方法
1.Info.plist中View controller-based status bar appearance被设置为YES
2.下面是代码
func navUI(self:UIViewController,title:String) {
self.navigationController?.navigationBar.isTranslucent = false
//修改导航栏按钮颜色 左箭头
self.navigationController?.navigationBar.tintColor = UIColor.white //修改返回按钮颜色
let item = UIBarButtonItem(title: "", style: .plain, target: self, action: nil)
self.navigationItem.backBarButtonItem = item
self.navigationItem.title = title
if #available(iOS 15.0, *) {
let barApp = UINavigationBarAppearance()
barApp.backgroundImage = UIImage(named: "导航状态栏底色")
//self.navigationController?.navigationBar.barStyle = .black //状态栏文字变成白色
barApp.shadowColor = .clear //设置导航栏下面的分割线的颜色
barApp.titleTextAttributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.white]
self.navigationController?.navigationBar.standardAppearance = barApp
self.navigationController?.navigationBar.scrollEdgeAppearance = barApp
self.navigationController?.navigationBar.overrideUserInterfaceStyle = .dark
self.navigationController?.navigationBar.barStyle = .black //状态栏文字变成白色
}else{
self.navigationController?.navigationBar.barStyle = .black //状态栏文字变成白色
self.navigationController?.navigationBar.shadowImage = UIImage()//隐藏导航栏跟界面的分割线
//关闭导航栏半透明效果
self.navigationController?.navigationBar.titleTextAttributes =
[NSAttributedString.Key.foregroundColor:UIColor.white//设置颜色
,NSAttributedString.Key.font:UIFont.boldSystemFont(ofSize: 20)//设置字体
,NSAttributedString.Key.backgroundColor:UIColor.clear//背景色
]
//修改导航栏背景图片(使用代码动态生成的纯色图片)
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "导航状态栏底色"), for: .default)
}
}
关键代码
//状态栏文字变成白色
self.navigationController?.navigationBar.overrideUserInterfaceStyle = .dark
self.navigationController?.navigationBar.barStyle = .black //状态栏文字变成白色
//状态栏文字变成默认
self.navigationController?.navigationBar.overrideUserInterfaceStyle = .light self.navigationController?.navigationBar.barStyle = .default