最近用 flutter 開發(fā)一個項目,在手機升到 ios 13 ,系統(tǒng)使用暗黑模式之后, 狀態(tài)欄字體顏色一直是白色的,官方目前還沒有適配完全, 在 github 上查了下,找到了臨時解決方案
首先 在 Info.plist 文件里面 設置 View controller based status bar appearance 字段為 yes,
然后在 AppDelegate 文件里面 最下面貼上如下代碼
let kOverlayStyleUpdateNotificationName = "io.flutter.plugin.platform.SystemChromeOverlayNotificationName"
let kOverlayStyleUpdateNotificationKey = "io.flutter.plugin.platform.SystemChromeOverlayNotificationKey"
extension FlutterViewController {
private struct StatusBarStyleHolder {
static var style: UIStatusBarStyle = .default
}
open override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(
self,
selector: #selector(appStatusBar(notification:)),
name: NSNotification.Name(kOverlayStyleUpdateNotificationName),
object: nil
)
}
open override var preferredStatusBarStyle: UIStatusBarStyle {
return StatusBarStyleHolder.style
}
@objc private func appStatusBar(notification: NSNotification) {
guard
let info = notification.userInfo as? Dictionary<String, Any>,
let statusBarStyleKey = info[kOverlayStyleUpdateNotificationKey] as? Int
else {
return
}
if #available(iOS 13.0, *) {
StatusBarStyleHolder.style = statusBarStyleKey == 0 ? .darkContent : .lightContent
} else {
StatusBarStyleHolder.style = statusBarStyleKey == 0 ? .default : .lightContent
}
setNeedsStatusBarAppearanceUpdate()
}
}