Swiftでウィンドウを追加する(SceneDelegateを使っている状態で)

Swift Swift
Swift

iOSアプリの作成する際に、iPhoneでもiPadでもウィンドウの概念があります。
特に意識せずに既存のウィンドウだけでも作ることができるので、意識することがない場合もありますが。

AppDelegateでのウィンドウ追加ではなく、SceneDelegateで使用する際のやり方のメモです。

追加自体はめちゃくちゃ簡単でした。

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        
    // ウィンドウ追加
    var newWindow: UIWindow = UIWindow(windowScene: windowScene)
    var newViewController: UIViewController = UIViewController()
    
    // ViewControllerを指定
    newWindow?.rootViewController = newViewController
    // Windowの重なり順を指定
    newWindow?.windowLevel = UIWindow.Level.normal + 100
    // Windowを表示する
    newWindow?.makeKeyAndVisible()
    
}

これだけの指定でウィンドウが追加されます。

追加したウィンドウ、ビューコントローラーに対して、いつも通りViewを配置したりすることでレイアウトを作成できます。

AppDelegateからSeneDelegateを使用するようになった際に、上記のコードでウィンドウを追加することができます。

コメント

タイトルとURLをコピーしました