iOS16以前で縦から横への変更
HogeController.swift
UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
的な感じでやっていたのですが、iOS16のシミュレータで効かなくなったようなので変更することにしました。
iOS16向け対応
AppDelegate と レイアウトを変更したい画面のViewController に修正を追加しました。
ViewControllerでは画面表示前でいいかと思ったので、とりあえずviewDidLoadに入れてみました。
AppDelegate.swift
var orientation: UIInterfaceOrientationMask = .portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return orientation
}
HogeViewController.swift
override func viewDidLoad() {
if #available(iOS 16.0, *) {
(UIApplication.shared.delegate as? AppDelegate)?.orientation = .landscapeRight
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: .landscapeRight))
setNeedsUpdateOfSupportedInterfaceOrientations()
}else{
UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
}
}