ios - 如何始终纵向加载应用程序?

标签 ios swift orientation

<分区>

应用程序在启动时必须是纵向的,但在我可以旋转之后。

如何做到这一点,也许以编程方式?

最佳答案

您可以像这样在您的 appDelegate 中为方向创建一个扩展:

protocol MyAppDelegate: class {
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
}


extension AppDelegate: MyAppDelegate {
    func application(_ application: UIApplication,
                     supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return shouldRotate ? .allButUpsideDown : .portrait
    }
}

在您想要允许旋转的 viewController 中,您应该在 viewDidLoad 方法中实现它:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = true

并在 viewWillDisappear 上再次删除它:

override func viewWillDisappear(_ animated: Bool) {
    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.shouldRotate = false
}

关于ios - 如何始终纵向加载应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48864001/

相关文章:

ios - 如果再次点击 TableView 中的单元格,UIAlertController 会给出 sigabrt 错误

ios - Swift EXC_BAD_ACCESS(代码=2,地址=0x42)

ios - Swift 在 UITableView 中创建分页

ios - 当方向改变时,效果不适用(swift3)

Android VideoView 不播放纵向

objective-c - UIImageView 以编程方式调整方向更改为横向模式

ios - Tableview 搜索栏不显示任何数据并且在我运行时有时会出现一些错误

ios - UIAlertController 不是窗口层次结构

swift - Core Data 中的哪些查询可以从属性的 R-Tree 索引中受益?

ios - 使用alamofire上传图片