ios - 将屏幕从纵向转换为横向它在从左到右旋转时不会保持横向,反之亦然
问题描述
我确实根据屏幕要求更改了 uidevice 的方向。
例如屏幕A,我只需要制作肖像。
屏幕 B 它应该是横向的,我做到了。
但是在返回屏幕 A 并再次进入屏幕 B 并将设备从左向右或从右向左旋转后,屏幕 B 的方向变为纵向。
如果有人对此有解决方案,请发表评论。
在这里,我将解释我在项目中所做的整个场景:
AppDelegate.swift
var orientationLock = UIInterfaceOrientationMask.portrait //Defined variable for orientation
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .allButUpsideDown : .portrait
}
在 A 类
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
Global.appDelegate.shouldRotate = false
}
在 B 类
DispatchQueue.main.async {
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
Global.appDelegate.shouldRotate = false
现在我尝试在所有旋转中制作 B 屏幕横向,为此我尝试了以下方法:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: nil, completion: {
_ in
if UIDevice.current.orientation == .portrait || UIDevice.current.orientation == .portraitUpsideDown {
// Global.AppUtility.lockOrientation(.landscape)
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
Global.appDelegate.shouldRotate = true
}
})
}
但是这种方法只在第一次起作用,在选择 A 并再次进入屏幕 B 后,它保持纵向。
解决方案
推荐阅读
- amazon-web-services - AspNetCore 3.1 Web API serverless - 如何管理延迟连接初始化
- python - 矩阵列中的python熊猫计数出现
- discord.js - 使用突击队踢成员
- scipy - 在 scipy.integrate 中跟踪 solve_ivp 函数的成功步骤
- java - Multiplication by a view matrix does not affect the resulting value in OpenGL ES 2.0/3.0
- python-3.x - 在字典中搜索列表作为键
- python - 使用 boto3 读取 S3 对象时引发内存不足错误
- azure-blob-storage - 如何计算 azure blob 中的不同文件
- odbc - PowerBI Desktop with Impala - 如何选择架构?
- java - 当用户单击按钮时在按钮上显示图像