我有一个电影视图压缩在右下视图与肖像模式。当用户展开电影视图时,电影视图将在景观模式下扩展到全屏。我也想锁定电影视图到景观模式时,全屏,无论设备是什么方向。
注意:我所有的其他视图都处于纵向模式。
我用这段代码引用了这个post,
应用程序设置

AppDelegate.swift
internal var shouldRotate = false
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .allButUpsideDown : .portrait
}视图控制器,
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)在设置方向之前,我将shouldRotate设置为true,使视图可以更改为景观模式。在设置方向之后,我将shoudRotate设置为false以禁用旋转。然后我测试它,当我点击按钮,电影视图改变为景观,在它旋转我的设备后,电影视图改变为纵向,锁定到纵向模式,而不是景观模式。
发布于 2017-05-10 02:52:46
它是由这个功能引起的,
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
}视图控制器,
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
}https://stackoverflow.com/questions/43773075
复制相似问题