我總是習慣UIScreen.main.bounds.size在 iOS 上獲取螢屏尺寸并且作業正常。
現在(我相信在 iOS 16 中)當我嘗試使用 UIScreen 的“主”實體訪問螢屏大小時,會顯示一條警告:
“main”將在未來的 iOS 版本中棄用:改用通過背景關系找到的 UIScreen 實體:即 view.window.windowScene.screen
所以我的問題是:獲取螢屏尺寸的正確方法是什么?為簡單起見,假設我們使用的是 iPhone,因此只有一個螢屏可用。謝謝
uj5u.com熱心網友回復:
在此處使用GeometryReader- 檔案:https ://developer.apple.com/documentation/swiftui/geometryreader 。用法示例:
struct MyTestView: View {
var body: some View{
VStack{
GeometryReader{ geo in
VStack {
Text("Width: \(geo.size.width)")
Text("Height: \(geo.size.height)")
}
}
}
}
}
geo.size是CGSize這里的檔案:https ://developer.apple.com/documentation/corefoundation/cgsize 。
uj5u.com熱心網友回復:
從視圖控制器:
讓 screenSize = self.view.window?.windowScene?.screen.bounds.size
uj5u.com熱心網友回復:
試試這個:
let height = view.bounds.size.height
let width = view.bounds.size.width
print("Width size:", view.bounds.size.width, "Width size:", view.bounds.size.height)
輸出 iPhone 14 pro
output: Width size: 393.0 Height size: 852.0
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/535847.html
標籤:ios迅速迅捷
上一篇:顯示時重繪iOS小部件
