当我试图在我的控制器上只允许纵向时,我总是收到这个错误: Error Domain=UISceneErrorDomain Code=101 “视图控制器不支持请求的方向。请求:landscapeLeft;支持:纵向” UserInfo={NSLocalizedDescription=视图控制器不支持请求的方向。要求:景观左;支持:纵向}
I called this method:
func updateOrientation(orientation: UIInterfaceOrientationMask) {
if #available(iOS 16, *) {
DispatchQueue.main.async {
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
self.setNeedsUpdateOfSupportedInterfaceOrientations()
self.navigationController?.setNeedsUpdateOfSupportedInterfaceOrientations()
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: orientation)) { error in
print(error)
print(windowScene?.effectiveGeometry )
}
}
}
}
Did someone face the same issue ?
J.Y.527 找到了解决方案: https://stackoverflow.com/a/73735976/2858994
这看起来像是一种解决方法,但它解决了我的问题。
诀窍是在需要旋转时更新 AppDelegate 支持的方向。
在您的 AppDelegate 中:
var orientation: UIInterfaceOrientationMask = .all
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return orientation
}
然后在你的控制器中:
static func SwitchOrientation(orientation : UIInterfaceOrientationMask, viewController : UIViewController){
if #available(iOS 16.0, *) {
(UIApplication.shared.delegate as? AppDelegate)?.orientation = orientation
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: orientation))
UIApplication.navigationTopViewController()?.setNeedsUpdateOfSupportedInterfaceOrientations()
DispatchQueue.main.async {
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
viewController.setNeedsUpdateOfSupportedInterfaceOrientations()
viewController.navigationController?.setNeedsUpdateOfSupportedInterfaceOrientations()
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: orientation)) { error in
print(error)
print(windowScene?.effectiveGeometry ?? "")
}
}
} else{
UIDevice.current.setValue(orientation, forKey: "orientation")
}
}
是的,我遇到了同样的问题并解决了如下问题。提供的错误清楚地表明我们正在尝试旋转设备,我们限制它只能在应用程序之前某处的一种模式下使用。在我的例子中,我在 Appdelegate.swift 中实现了下面的方法
var myOrientation: UIInterfaceOrientationMask = .portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { return myOrientation }
所以限制它只能使用纵向模式,导致应用程序在横向模式下旋转失败。
通过改变所有人的方向,让它工作。
var myOrientation: UIInterfaceOrientationMask = .portrait
你可以这样试试,对我有用
- (void)forceOrientation:(UIDeviceOrientation)orientation {
if (@available(iOS 16.0, *)) {
UIInterfaceOrientationMask orientationMask = (1 << orientation);
[self setNeedsUpdateOfSupportedInterfaceOrientations];
UIWindowScene *windowScene = (UIWindowScene *)
[UIApplication sharedApplication].connectedScenes.allObjects.firstObject;
for (UIWindow *windows in windowScene.windows) {
[windows.rootViewController setNeedsUpdateOfSupportedInterfaceOrientations];
}
UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:orientationMask];
[windowScene requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:nil];
} else {
[[UIDevice currentDevice] setValue:@(orientation) forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];
}
}
在 Appdelegate 中尝试