首页 文章

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

提问于
浏览
0

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

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

我用这段代码引用了这个post

应用设置,

enter image description here

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)

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

1 回答

  • 0

    它是由这个功能引起的,

    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
    }
    

相关问题