[英]Swift 5: Status Bar Color Management with Orientation Changes
IOS 15,Xcode 13.2.1,Swift 5
我希望在我的多視圖 controller 應用程序中管理狀態欄顏色。 我已成功找到如何更改每個 controller 和縱向的狀態欄顏色。 問題是當轉向橫向時,狀態欄消失,縱向模式下狀態欄大小的矩形覆蓋導航區域的左側。
這是我的代碼和用法:
func statusBarColor() {
if #available(iOS 13.0, *) {
let statusBar2 = UIView()
if UIApplication.shared.currentScene?.statusBarManager!.statusBarFrame != nil {
statusBar2.frame = (UIApplication.shared.currentScene?.statusBarManager!.statusBarFrame)!
statusBar2.backgroundColor = UIColor.init(named: "BackGroundColor")
UIApplication.shared.windows.first?.addSubview(statusBar2)
}
} else {
let statusBar2: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
statusBar2.backgroundColor = UIColor.init(named: "BackGroundColor")
}
}
使用:在viewWillAppear中調用function。
問題圖片:橫向模式下的狀態欄(已覆蓋導航欄)
感謝馬特在問題評論中的提示,我找到了一個可行的答案。 應該注意的是,由於不相關的錯誤,這在我的應用程序中可能是必需的,這可以被認為是一個補丁,而不是錯誤的解決方案。 此代碼創建一個具有背景顏色的視圖並將其放置在狀態欄中。 當設備處於橫向模式以及離開當前的 viewController 時,視圖會被隱藏。
這適用於 Swift 5、iOS 15、Xcode 13.2.1
class TableViewController: UITableViewController {
let statusBar1 = UIView()
override func viewWillAppear(_ animated: Bool) {
statusBarColor()
statusBar1.isHidden = false
}
override func viewWillDisappear(_ animated: Bool) {
statusBar1.isHidden = true
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.current.orientation.isLandscape {
statusBar1.isHidden = true
} else {
statusBar1.isHidden = false
}
}
func statusBarColor() {
if #available(iOS 13.0, *) {
self.statusBar1.frame = (UIApplication.shared.currentScene?.statusBarManager!.statusBarFrame)!
self.statusBar1.backgroundColor = UIColor.init(named: "AccentColor")
UIApplication.shared.windows.first?.addSubview(statusBar1)
} else {
let statusBar2: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
statusBar2.backgroundColor = UIColor.init(named: "AccentColor")
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.