[英]How to set style of the statusbar embedded in a navigation controller on iOS13?
[英]How to adapt ios13 statusbar?
當我在 ios13 xcode11 beta 上運行我的項目時。
[UIApplication sharedApplication].statusBarFrame.size.height
代碼返回 0。
我應該怎么做才能使其適應 ios13?
使用UIStatusBarManager獲取iOS13中的statusBar高度:
UIStatusBarManager *manager = [UIApplication sharedApplication].keyWindow.windowScene.statusBarManager;
CGFloat height = manager.statusBarFrame.size.height;
SceneDelegate.swift文件
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
if let statusBarFrame = windowScene.statusBarManager?.statusBarFrame {
print(statusBarFrame)
}
}
正如彼得在Tamarous 的回答中指出的那樣,不推薦使用keyWindow屬性,但是假設您使用的是單個窗口,您也可以使用:
CGSize statusBarSize = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager.statusBarFrame.size
如果您需要訪問第n 個窗口,您可以采用類似的方式:
NSInteger i = 0;
CGSize statusBarSize = [UIApplication sharedApplication].windows[i].windowScene.statusBarManager.statusBarFrame.size;
解決方案:
let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
lazy var statusBarHeight = window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
解釋 :
您不能在let
使用window
,因為在創建該屬性時它不存在,因為它屬於 self. 所以在init
, self
還沒有完成。 但是,如果您使用lazy var
,則self
及其屬性window
將在您需要時准備就緒。
或另一種解決方案使用singleton
:
struct AppConstants {
static let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
static let statusBarHeight = window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.