安全エリアのインセットの上部と下部の高さを取得する 質問する

安全エリアのインセットの上部と下部の高さを取得する 質問する

安全でない領域の上部と下部の高さを取得する最も適切な方法は何でしょうか?

ここに画像の説明を入力してください

ベストアンサー1

これを試して :

Objective Cの場合

if (@available(iOS 11.0, *)) {
    UIWindow *window = UIApplication.sharedApplication.windows.firstObject;
    CGFloat topPadding = window.safeAreaInsets.top;
    CGFloat bottomPadding = window.safeAreaInsets.bottom;
}

Swift

if #available(iOS 11.0, *) {
    let window = UIApplication.shared.keyWindow
    let topPadding = window?.safeAreaInsets.top
    let bottomPadding = window?.safeAreaInsets.bottom
}

Swift - iOS 13.0以降

// Windows 配列の最初の要素を KeyWindow として使用します (非推奨)

if #available(iOS 13.0, *) {
    let window = UIApplication.shared.windows.first
    let topPadding = window.safeAreaInsets.top
    let bottomPadding = window.safeAreaInsets.bottom
}

Swift - iOS 15.0以降

// 現在のシーンのキーウィンドウを使用する

if #available(iOS 15.0, *) {
    let window = UIApplication.shared.currentScene?.keyWindow
    let topPadding = window.safeAreaInsets.top
    let bottomPadding = window.safeAreaInsets.bottom
}

おすすめ記事