StatusBarBackgroundColor.swift 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. //
  2. // StatusBarBackgroundColor.swift
  3. // naiyoup
  4. //
  5. // Created by C Auto on 2021/6/4.
  6. //
  7. import SwiftUI
  8. extension UIApplication {
  9. class var statusBarBackgroundColor: UIColor? {
  10. get {
  11. if #available(iOS 13.0, *) {
  12. let statusBar = UIView(frame: UIApplication.shared.windows[0].windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
  13. return statusBar.backgroundColor
  14. } else {
  15. let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
  16. return statusBar.backgroundColor
  17. }
  18. }
  19. set {
  20. if #available(iOS 13.0, *) {
  21. let statusBar = UIView(frame: UIApplication.shared.windows[0].windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
  22. if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
  23. statusBar.backgroundColor = newValue
  24. }
  25. UIApplication.shared.windows[0].addSubview(statusBar)
  26. } else {
  27. let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
  28. if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
  29. statusBar.backgroundColor = newValue
  30. }
  31. }
  32. }
  33. }
  34. }