在iOS 16之前,在景观中呈现单一屏幕是很好的肖像应用。工作代码如下。
备注:整个应用程序只处于肖像模式。
override public var shouldAutorotate: Bool {
return false
}
override public var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeLeft
}
override public var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .landscapeLeft
}我找到了解决方案,但是它是针对UIWindowScene的,但是,我需要UIWindow中的解决方案,,我需要帮助,在iOS 16中修复它。
Xcode - 14.0,iOS - 16.0,模拟器- 14 Pro
如果有人需要的话我可以准备演示。
发布于 2022-10-06 14:39:41
经过多次尝试,我想出了一个简单的解决方案。正如我在问题中提到的,我的整个应用程序都是在纵向模式,只有一个屏幕,我想在景观中呈现。
此代码不要求任何外部窗口为makeKeyAndVisible**.**,如果您使用额外的窗口来表示,则需要为iOS 16单独写入以拒绝。
在以前版本的iOS 16中使用的旧代码将保持不变,不会改变。
魔法线如下所示。
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let _ = window?.rootViewController?.presentedViewController as? LandscapeChartVC {
if #available(iOS 16.0, *) {
return .landscapeLeft
} else {
return .portrait
}
} else {
return .portrait
}
}我已经在应用程序代表的supportedInterfaceOrientationsFor中识别了我的景观视图控制器。
你可以改变单词presentedViewController来得到你的控制器。就是这样。
添加了iPad对所有3或4方向的支持,如下所示:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if UIDevice.IS_IPAD {
return .allButUpsideDown
} else {
if let _ = window?.rootViewController?.presentedViewController as? LandscapeChartVC {
if #available(iOS 16.0, *) {
return .landscapeLeft
} else {
return .portrait
}
} else {
return .portrait
}
}如果需要iPad应用程序锁定方向,您可以遵循iPhone /上面的代码。
这个想法想出了答案,并感谢大家的共同兴趣。如果还有人得到更好的解决方案,我很乐意更新。
发布于 2022-09-22 04:38:06
这可以通过两种方式来完成。
1 .personally我更喜欢这种方式
1.1将此函数保留在AppDelegate中以处理定向(这是必须的)
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return .all
}1.2,在ViewController中,您想要向力的方向,转到视图控制器,并在变量声明部分中添加这些行
var forceLandscape: Bool = false
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
forceLandscape ? .landscape : .portrait
}我们将更新forceLandscape,使其得到更新,然后supportedInterfaceOrientations也将得到更新。
1.3我们在这里设置了更新forceLandscape的触发器(我们可以在按钮中添加这些代码行,用于处理IOS 16力旋转)
if #available(iOS 16.0, *) {
self.forceLandscape = true
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene else { return }
self.setNeedsUpdateOfSupportedInterfaceOrientations()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: {
windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: .landscapeRight)){
error in
print(error)
print(windowScene.effectiveGeometry)
}
})这将更新forceLandscape,,因此它将检查方向并根据其进行更新。
代码行上的是一种方式,另一种方式是在下面给出
2.另一种方法是在AppDelegate类中更新方向:
2.1将此函数和属性保留在AppDelegate中以处理方向(这是必须的)
var orientation : UIInterfaceOrientationMask = .portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return orientation
}2.2在视图控制器按钮操作中,我们可以更新propperty
@IBAction func buttonAction(_ sender: Any) {
let appDel = UIApplication.shared.delegate as! AppDelegate
appDel.orientation = .landscape
if #available(iOS 16.0, *) {
DispatchQueue.main.async {
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
self.setNeedsUpdateOfSupportedInterfaceOrientations()
self.navigationController?.setNeedsUpdateOfSupportedInterfaceOrientations()
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: .landscape)) { error in
print(error)
print(windowScene?.effectiveGeometry ?? "")
}
}
}else{
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
}发布于 2022-09-16 08:46:06
我在iOS 16发行说明中找到了一些相关的内容。
在UIKit中有一些不推荐的地方:
贬义词 已不再支持UIViewController shouldAutorotate。setNeedsUpdateOfSupportedInterfaceOrientations.已不再推荐UIViewController attemptRotationToDeviceOrientation,取而代之的是UIViewController attemptRotationToDeviceOrientation。 解决方案:依赖于shouldAutorotate的应用程序应该使用视图控制器supportedInterfaceOrientations来反映它们的首选项。如果支持的方向发生变化,请使用`-[UIViewController setNeedsUpdateOfSupportedInterface
我想你可能得用setNeedsUpdateOfSupportedInterface.
https://stackoverflow.com/questions/73727291
复制相似问题