问题描述
有人可以帮忙吗。我的设备支持纵向模式。但是只有一个视图控制器可以使用横向模式。当我打开支持横向模式的视图控制器并单击 tabBar 项时,设备返回纵向模式但视图仍处于横向模式。
这些是我的 appDelegate 代码:
//Orientation Variables
var myOrientation: UIInterfaceOrientationMask = .portrait
var orientationLock = UIInterfaceOrientationMask.portrait
func application(_ application: UIApplication,supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return self.orientationLock
}
struct AppUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}
static func lockOrientation(_ orientation: UIInterfaceOrientationMask,andRotateto rotateOrientation:UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue,forKey: "orientation")
}
}
这是我的景观支持的 ViewController :
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
AppDelegate.AppUtility.lockOrientation(.allButUpsideDown)
}
我还有右导航栏按钮。当用户点击按钮时,屏幕会转动 我有两个旋转功能
//MARK: Rotate Device to right
func rotatetoLandsScapeDevice(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.orientationLock = .landscapeRight;
appDelegate.orientationLock = .allButUpsideDown;
UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue,forKey: "orientation")
UIView.setAnimationsEnabled(true)
buttonClicked = false
}
//MARK: Rotate Device to portrait
func rotatetoPotraitScapeDevice(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.orientationLock = .portrait;
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue,forKey: "orientation")
UIView.setAnimationsEnabled(true)
buttonClicked = true
}
当视图消失时,我转向纵向模式:
override func viewWilldisappear(_ animated: Bool) {
super.viewWilldisappear(animated)
AppDelegate.AppUtility.lockOrientation(.portrait)
UIView.setAnimationsEnabled(true)
}
当我单击返回时,旋转工作正常,它会旋转纵向模式。但是当我单击标签栏项目视图时,如下所示:
解决方法
在我的 TabBarController 类中,我检测用户何时单击项目。然后我改变设备旋转。
override func tabBar(_ tabBar: UITabBar,didSelect item: UITabBarItem) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.orientationLock = .portrait;
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue,forKey: "orientation")
UIView.setAnimationsEnabled(true)
}