Code: Select all
class AppDelegate {
public var orientationLock: UIInterfaceOrientationMask = .all
public func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return orientationLock
}
}
public enum ScreenUtil {
public static func forceOrientation(_ mask: UIInterfaceOrientationMask) {
let delegate = UIApplication.shared.delegate as! AppAppDelegate
delegate.orientationLock = mask
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: mask))
NAV_VC?.setNeedsUpdateOfSupportedInterfaceOrientations()
}
}
Code: Select all
open class MyVC: UIViewController {
private let supportedOrientationGetter: () -> UIInterfaceOrientationMask
open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
ScreenUtil.forceOrientation(supportedOrientationGetter())
}
}
Beachten Sie, dass dieser Code das Flag UIRequiresFullScreen = YES in Info.plist erfordert und außerdem die Schnittstellenausrichtungen in Info.plist nur .portrait enthalten, was ein Dummy-Wert ist. Die tatsächliche Ausrichtung wird im obigen Code festgelegt.
Dieser Code funktionierte zuvor gut, schlägt jedoch unter iPadOS 26 fehl (iPhone funktioniert immer noch). Auch UIRequiresFullScreen ist veraltet, daher möchte es trotzdem entfernt werden.
Ich habe die Anweisung aus dem Hinweis zur veralteten Version von UIRequiresFullScreen befolgt: https://developer.apple.com/documentati ... FullScreen. Dies habe ich mit der API „prefersInterfaceOrientationLocked“ und „supportedInterfaceOrientations“ versucht, aber es hat nicht funktioniert.
Im Beispiel unten habe ich eine ViewController-Klasse, die im Hochformat sein sollte, und sie wird dann an eine LandscapeVC-Klasse weitergeleitet, die im Querformat sein sollte.
Code: Select all
import UIKit
class ViewController: UIViewController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var prefersInterfaceOrientationLocked: Bool {
true
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Hello"
let item = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(clicked))
navigationItem.rightBarButtonItem = item
}
@objc
func clicked() {
let vc = LandscapeVC()
self.navigationController!.pushViewController(vc, animated: true)
}
}
class LandscapeVC: UIViewController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscape
}
override var prefersInterfaceOrientationLocked: Bool {
true
}
}
Außerdem einige zusätzliche Informationen dazu, warum ich für diesen speziellen Bildschirm Querformat benötige – weil es ein Querformat-Minispiel ist, das Gyroskop verwendet. Für den Rest der App funktioniert jede Ausrichtung, aber für dieses Minispiel muss ich das Querformat festlegen.
Mobile version