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

时间:2017-05-04 02:54:12

标签: ios swift user-interface orientation

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

注意:我的所有其他观点都处于纵向模式。

我已使用此代码

引用此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 :(得分:0)

由此功能引起,

sched_switch

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

可行的代码,

AppDelegate.swift

.landscape

查看控制器,

func application(_ application: UIApplication,
                 supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return shouldRotate ? .landscape : .portrait
}
相关问题