背景
画面向きに合わせて画面回転をさせる処理が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()
}
}