更改iOS13中的状态栏背景色
使用iOS13,我无法再更改状态栏的背景色,因为无法再使用“键的值”访问状态栏。 有人知道这是怎么可能的吗?或者有人知道在iOS13的最终版本中这是可能的吗 我遇到了不同的建议,比如使用UIApplications StatusBarView(在xcode 11、beta 7中不再可访问)或使用statusbarmanager。两者都不允许访问状态栏更改iOS13中的状态栏背景色,ios,objective-c,statusbar,ios13,Ios,Objective C,Statusbar,Ios13,使用iOS13,我无法再更改状态栏的背景色,因为无法再使用“键的值”访问状态栏。 有人知道这是怎么可能的吗?或者有人知道在iOS13的最终版本中这是可能的吗 我遇到了不同的建议,比如使用UIApplications StatusBarView(在xcode 11、beta 7中不再可访问)或使用statusbarmanager。两者都不允许访问状态栏 将statusBar:UIView=UIApplication.shared.value(forKey:“statusBar”)设为!UIView
将statusBar:UIView=UIApplication.shared.value(forKey:“statusBar”)设为!UIView
if statusBar.responses(to:#选择器(setter:UIView.backgroundColor)){
statusBar.backgroundColor=
}
我希望状态栏得到我需要的背景色
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .blue
self.navigationController?.navigationBar.scrollEdgeAppearance = appearance
我希望,这会对你有所帮助
if let statusBar = UIStatusBarManager.self as? UIView {
if statusBar.responds(to: #selector(setter: UIView.backgroundColor)) {
statusBar.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
}
}
我希望这对你有用试试这个
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.backgroundColor = <insert your color here>
navigationBar.standardAppearance = navBarAppearance
navigationBar.scrollEdgeAppearance = navBarAppearance
}
if#可用(iOS 13.0,*){
让navBarAppearance=UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.titleTextAttributes=[.foregroundColor:UIColor.white]
navBarAppearance.largeTitleTextAttributes=[.foregroundColor:UIColor.white]
navBarAppearance.backgroundColor=
navigationBar.standardAppearance=导航栏外观
navigationBar.scrollEdgeAppearance=navBarAppearance
}
如果未使用NavigationController,则可以在ViewController中设置基本视图背景色
view.backgroundColor = .blue
override func viewDidAppear(_ animated: Bool) {
if #available(iOS 13, *)
{
let statusBar = UIView(frame: (UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame)!)
statusBar.backgroundColor = UIColor.systemBackground
UIApplication.shared.keyWindow?.addSubview(statusBar)
}
}
使用UIStatusBarManager的Objective-C版本:
UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
statusBar.backgroundColor = [UIColor whiteColor];
[[UIApplication sharedApplication].keyWindow addSubview:statusBar]
状态栏将尝试匹配应用程序的颜色
如果你有导航栏
self.navigationBar.barTintColor = UIColor.blue
or
UINavigationBar.appearance().barTintColor = UIColor.blue
如果没有导航栏
view.backgroundColor = UIColor.blue
如果您的背景是网络视图
webView.scrollView.backgroundColor = UIColor.blue
我可能缺少更多的案例请在基本视图控制器中调用此选项
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上使用此选项
view.backgroundColor = .blue
override func viewDidAppear(_ animated: Bool) {
if #available(iOS 13, *)
{
let statusBar = UIView(frame: (UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame)!)
statusBar.backgroundColor = UIColor.systemBackground
UIApplication.shared.keyWindow?.addSubview(statusBar)
}
}
您可以在“UIColor.systemBackground”中使用您的颜色我使用的是中央管理器类,在那里我设置了所有的UI颜色。我就是这样解决的。您可以选择适合您需要的部分解决方案:
let sharedApplication = UIApplication.shared
sharedApplication.delegate?.window??.tintColor = setYourColor
if #available(iOS 13.0, *) {
let statusBar = UIView(frame: (sharedApplication.delegate?.window??.windowScene?.statusBarManager?.statusBarFrame)!)
statusBar.backgroundColor = setYourColor
sharedApplication.delegate?.window??.addSubview(statusBar)
} else {
// Fallback on earlier versions
sharedApplication.statusBarView?.backgroundColor = setYourColor
}
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
代码适用于Swift 5+和iOS 13+ 为了更改状态栏的背景颜色,我创建了UIViewController的基类,以便可以在所有视图控制器中继承相同的代码 在UIViewController扩展中添加了“statusBarColorChange()”
extension UIViewController {
func statusBarColorChange() {
if #available(iOS 13.0, *) {
let statusBar = UIView(frame: UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
statusBar.backgroundColor = .appNavigationThemeColor
statusBar.tag = 100
UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.addSubview(statusBar)
} else {
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = .appNavigationThemeColor
}
} }
创建基类并在其他类中继承
class BaseClass: UIViewController {
override func viewWillAppear(_ animated: Bool) {
self.statusBarColorChange()
}
}
class ChatListViewController: BaseClass {}
希望会有所帮助:)贾斯普里特科尔的答案很有效;但是,重置其他视图控制器的状态栏颜色可能非常困难(特别是如果需要状态栏下方的图像内容)。将颜色设置为.clear无效 更简单的解决方案是清除导航栏:
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.backgroundColor = .clear
navigationController?.navigationBar.isTranslucent = true
然后将UIView(将背景颜色设置为所需的颜色)添加到视图控制器,并将其约束设置为superview的最顶部(而不是边距)
使用此技术,您的颜色将显示在状态栏下方,您无需干扰statusBarManager(其属性均为只读)。检查此答案。感谢@SAXENA-这似乎需要导航栏来设置外观。我正在寻找一种没有导航栏的方法。@Nij请参阅此以获取帮助:不,它没有帮助,固定导航栏颜色只有不状态栏颜色不,它没有帮助,固定导航栏颜色只有不状态栏颜色我需要在obj c not swiftAs中,据我所知,目前在Objective-c@VinodhCast from中没有方法这样做“UIStatusBarManager.Type”到不相关的类型“UIView”总是失败是否需要将其添加到每个ViewController?如果我们想在整个应用程序中执行该怎么办?不工作,因为iOS 13.0中不推荐使用“keyWindow”:不应用于支持多个场景的应用程序,因为它会在所有连接的场景中返回一个关键窗口显示在ViewWillEnglishe()时删除状态栏被称为?@ShubhamGupta您找到了在整个应用程序中执行的解决方案吗?注意:对于不使用SceneDelegate的应用程序,只需使用
[UIApplication sharedApplication]。statusBarFrame