我在右下视口(viewport)以纵向模式压缩了电影 View 。当用户展开电影 View 时,电影 View 将在横向模式下扩展到全屏。我还想在全屏时将电影 View 锁定为横向模式,无论设备处于什么方向。
注意:我的所有其他 View 都是纵向模式。
我引用了这个post使用此代码,
应用程序设置,
AppDelegate.swift
internal var shouldRotate = false
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .allButUpsideDown : .portrait
}
View Controller ,
func expandPlayerWindow(button: UIButton) {
self.player.view.frame = CGRect(x:0, y:-20, width: UIScreen.main.bounds.maxY, height: UIScreen.main.bounds.maxX)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
print(appDelegate.shouldRotate)
print(self.supportedInterfaceOrientations)
appDelegate.shouldRotate = true // or false to disable rotation
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
print(appDelegate.shouldRotate)
print(self.supportedInterfaceOrientations)
appDelegate.shouldRotate = false
print(appDelegate.shouldRotate)
print(self.supportedInterfaceOrientations)
UIApplication.shared.isStatusBarHidden = false
}
日志,
false
UIInterfaceOrientationMask(rawValue: 26)
true
UIInterfaceOrientationMask(rawValue: 26)
false
UIInterfaceOrientationMask(rawValue: 26)
我在setorientation之前将shouldRotate设置为true,这使得 View 可以更改为横向模式。设置方向后,我将 shaudRotate 设置为 false 以禁用旋转。然后我测试它,当我单击按钮时,电影 View 更改为横向,之后我旋转设备,电影 View 更改为纵向,并锁定为纵向模式而不是横向模式。
最佳答案
是这个函数造成的,
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .allButUpsideDown : .portrait
}
将 .allButUpsideDown
更改为 .landscape
即可。
可行的代码,
AppDelegate.swift
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .landscape : .portrait
}
View Controller ,
func expandPlayerWindow() {
self.player.view.frame = CGRect(x:0, y:-20, width: UIScreen.main.bounds.maxY, height: UIScreen.main.bounds.maxX)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = true // or false to disable rotation
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
appDelegate.shouldRotate = true
UIApplication.shared.isStatusBarHidden = true
}
关于ios - 如何将 View 从纵向模式更改为横向模式并锁定它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43773075/