Site icon image技術メモ

[iOS]ステータスバーの高さを考慮してレイアウトを組む際の注意点

  • 以下処理は、ステータスバーが表示されているかどうかで返却値が変わる
// Before iOS 13
UIApplication.shared.statusBarFrame.height

// After iOS13
UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.windowScene?.statusBarManager?.statusBarFrame.height

  • ステータスバーの表示状態を問わず、ステータスバーの高さを考慮したレイアウトを組むために高さを確保したい場合は以下手法が考えられる
    • ステータスバーを表示した状態で取得した値を保持する
    • セーフエリアの値を用いる
      UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.safeAreaInsets.top
      
      // もしくは
      view.safeAreaInsets.top