Press "Enter" to skip to content

【SwiftUI】親ビューのサイズを取得する

GeometryReader を使用します。

公式ドキュメント: https://developer.apple.com/documentation/swiftui/geometryreader

struct ContentView: View {
    var body: some View {
        GeometryReader { proxy in
            VStack(alignment: .leading) {
                Text(String("\(proxy.size)")) // 親ビューのサイズ
                Text(String("\(safeArea())")) // セーフエリア
            }
        }
    }
}

以下は一番上の親(ContentView)で iPhone 13 で試した時の表示です。iPhone13 は画面サイズ 6.1 inch(390×844)、そのうち縦のセーフエリアが top: 47、bottom: 34 を占めるので、763という結果になっています。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です