Ios 如何将视图从纵向模式更改为横向模式并将其锁定?

Ios 如何将视图从纵向模式更改为横向模式并将其锁定?,ios,swift,user-interface,orientation,Ios,Swift,User Interface,Orientation,我有一个电影视图压缩在右下视口与肖像模式。当用户展开电影视图时,电影视图将以横向模式扩展到全屏。我还想在全屏时将电影视图锁定为横向模式,无论设备的方向如何 注意:我的所有其他视图都处于纵向模式 我用这个代码引用了这个 应用程序设置 AppDelegate.swift internal var shouldRotate = false func application(_ application: UIApplication, supportedInterfa

我有一个电影视图压缩在右下视口与肖像模式。当用户展开电影视图时,电影视图将以横向模式扩展到全屏。我还想在全屏时将电影视图锁定为横向模式,无论设备的方向如何

注意:我的所有其他视图都处于纵向模式

我用这个代码引用了这个

应用程序设置

AppDelegate.swift

internal var shouldRotate = false

func application(_ application: UIApplication,
                 supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return shouldRotate ? .allButUpsideDown : .portrait
}
func application(_ application: UIApplication,
                 supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return shouldRotate ? .landscape : .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
}
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
}
日志


在设置方向之前,我将shouldRotate设置为true,这样可以将视图更改为横向模式。在设置方向后,我将shoudRotate设置为false以禁用旋转。然后我测试它,当我点击按钮时,电影视图变为横向,旋转设备后,电影视图变为纵向,并锁定为纵向模式而不是横向模式。

这是由此功能引起的

func application(_ application: UIApplication,
                 supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return shouldRotate ? .allButUpsideDown : .portrait
}
.allButUpsideDown
更改为
。横向
即可

可操作的代码

AppDelegate.swift

internal var shouldRotate = false

func application(_ application: UIApplication,
                 supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return shouldRotate ? .allButUpsideDown : .portrait
}
func application(_ application: UIApplication,
                 supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return shouldRotate ? .landscape : .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
}
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
}

电影视图的父视图如何。你是否也在阻止它们旋转?是的,我认为阻止它们比仅电影视图更容易。你的电影视图是视图控制器的视图吗?视图控制器是否在每种模式下返回shouldAutorotate和supportedInterfaceOrientations的正确值?电影视图有父视图,和父视图(如果是视图控制器的视图)。我已经检查了这些,并在问题中进行了编辑。检查一下。