ios - 如何将 View 从纵向模式更改为横向模式并锁定它?

标签 ios swift user-interface orientation

我在右下视口(viewport)以纵向模式压缩了电影 View 。当用户展开电影 View 时,电影 View 将在横向模式下扩展到全屏。我还想在全屏时将电影 View 锁定为横向模式,无论设备处于什么方向。

注意:我的所有其他 View 都是纵向模式。

我引用了这个post使用此代码,

应用程序设置,

enter image description here

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/

相关文章:

swift - tvOS - pressesEnded 在启动时不起作用

Java 桌面 - 如何将数据库访问与 UI 线程分开?

ios - IOS 的表单容器组件

SwiftUI - TabView with NavigationView 生成灰色区域

php - 如何在 MySQL 数据库中存储对象的颜色数据?

iOS 单独或在一个存档中下载每个文件

xcode - 如何初始化相互依赖的属性

ios - 当子类化 UIView 并将其添加到界面构建器中时,不会调用 drawRect

ios - 为什么在检查 Firebase 中是否存在用户名时从 textField 中删除文本时应用程序崩溃?

ios - 无法获取 NSData