🪟 UIWindowとは?

UIWindow は、iOSアプリの画面表示の最上位に位置するコンテナです。アプリの ビュー階層の基点 となり、画面描画タッチイベントの伝達 を担います。


…読み込み中…

📌 UIWindowの主な役割

🧱 UIWindowの基本実装

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  var window: UIWindow?

  func scene(_ scene: UIScene,
             willConnectTo session: UISceneSession,
             options connectionOptions: UIScene.ConnectionOptions) {

    guard let windowScene = (scene as? UIWindowScene) else { return }

    let window = UIWindow(windowScene: windowScene)
    window.rootViewController = YourRootViewController()
    self.window = window
    window.makeKeyAndVisible()
  }
}

UIWindowScene と連携して、iOS 13以降は SceneDelegate で設定します。

📐 UIWindowのプロパティ

📋 複数ウィンドウの管理

iPadでは、1つのアプリで複数の UIWindow を扱うことが可能です。これは UIScene を利用して管理されます。

注意: 通常のiPhoneアプリではウィンドウは基本的に1つのみです。

🛠 UIKitベースのアプリ構成

// AppDelegateでの旧来のウィンドウ生成(iOS 12以前)
func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

  window = UIWindow(frame: UIScreen.main.bounds)
  window?.rootViewController = ViewController()
  window?.makeKeyAndVisible()
  return true
}

🔗 関連リンク