0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

setNeedsUpdateOfSupportedInterfaceOrientationsを利用した画面回転

Posted at

背景

画面向きに合わせて画面回転をさせる処理がiOS16から非推奨となったので対応したメモです。

iOS15以前

attemptRotationToDeviceOrientation※Deprecated

UIViewController.attemptRotationToDeviceOrientation()

iOS16以降

setNeedsUpdateOfSupportedInterfaceOrientations

let window = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first?.windows.filter { $0.isKeyWindow }.first
window?.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations()

どちらも対応するとき

OrientationSettings.swift
    /// デバイスの向きに合わせて画面を回転させる
    private func rotateScreen() {
        if #available(iOS 16.0, *) {
            let window = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first?.windows.filter { $0.isKeyWindow }.first
            window?.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations()
        } else {
            UIViewController.attemptRotationToDeviceOrientation()
        }
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?