【问题标题】:Unable to lock rotation for one view controller in IOS10在 IOS10 中无法锁定一个视图控制器的旋转
【发布时间】:2019-11-18 16:47:18
【问题描述】:

背景

我有一个使用 AVFoundation 的应用程序,以便拥有一个自定义相机。这发生在OCRViewController。当我拍照时,我会将捕捉到的照片发送到不同的视图ImagePreviewViewController

我正在使用Xcode 10.2.1 (10E1001)Swift 5

目标

我想要实现的是将ImagePreviewViewController 的方向锁定为图像的原始方向。我已经知道如何获取图像的方向,但我无法锁定视图的方向。

我得到这样的图像旋转:let imageOri = capturedImage?.imageOrientation

我尝试了什么?

我在和其他几个来源尝试了接受的答案:

How to lock orientation just for one view controller?

How to lock orientation of one view controller to portrait mode only in Swift

https://www.hackingwithswift.com/example-code/uikit/how-to-lock-a-view-controllers-orientation-using-supportedinterfaceorientations

Handling View Rotation 下阅读https://developer.apple.com/documentation/uikit/uiviewcontroller#//apple_ref/occ/instm/UIViewController/supportedInterfaceOrientations 的文档,说明如下:

在编写此查询时,我也尝试了许多建议的解决方案,但是,大多数似乎使用以下方法(或它的变体),它对我不起作用。

override func supportedInterfaceOrientations() -> Int {
    return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}

override func shouldAutorotate() -> Bool{
    return false
}

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    return UIInterfaceOrientation.Portrait
}

从 iOS 8 开始,所有与旋转相关的方法都已弃用。相反,旋转被视为视图控制器视图大小的变化,因此使用 viewWillTransition(to:with:) 方法报告。

但是,我不确定如何从这里开始。

有趣的代码sn-ps

下面的方法在我的OCRViewController里,这里我实例化ImagePreviewViewController,并附上截取的图片。

func displayCapturedPhoto(capturedPhoto : UIImage) {
    let imagePreviewViewController = storyboard?.instantiateViewController(withIdentifier: "ImagePreviewViewController") as! ImagePreviewViewController
    imagePreviewViewController.capturedImage = capturedPhoto
    navigationController?.pushViewController(imagePreviewViewController, animated: true)
}

使用ImagePreviewViewController 中的以下覆盖函数,我能够检测到视图控制器的方向。

override func viewDidAppear(_ animated: Bool) {
    if UIDevice.current.orientation.isLandscape {
        print("Landscape")
    } else {
        print("Portrait")
    }
}

【问题讨论】:

  • 我所做的是在 UIApplicationDelegate 中。声明两个变量var shouldSupportAllOrientation = truevar orientationLock = UIInterfaceOrientationMask.portrait 并在appDelegate 中使用supportedInterfaceOrientationsFor 返回orientationLock 变量。在您的视图控制器中 appDelegate?.shouldSupportAllOrientation = true appDelegate?.orientationLock = .portrait
  • @KamalUpasena 我刚刚尝试过,但遗憾的是它不起作用。我也这样做了:在我的 AppDelegate 中,我按照您的指定创建了两个变量。然后在 viewWillAppear 下的视图控制器中(也尝试过 viewDidLoad)我像你一样使用它,无论额外的代码如何,视图仍然会旋转。
  • 在 info.plist 中添加 4 项的方向支持
  • @KamalUpasena 你能详细说明一下吗?我不确定我是否理解这 4 项是什么。
  • 从常规添加设备方向为纵向、上下颠倒、横向左、横向右 -> 部署信息

标签: swift xcode uiviewcontroller ios10 swift5


【解决方案1】:

要限制一个屏幕的旋转,请使用它。

在 AppDelegate 中

var restrictRotation = Bool()

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if !restrictRotation {
        return .portrait
    } else {
        return .all
    }
}

在您的视图控制器中添加该功能,

func restrictRotation(restrict : Bool) -> Void {
    let appDelegate = UIApplication.shared.delegate as? AppDelegate
    appDelegate?.restrictRotation = restrict
}

在 ViewDidload() 方法中,调用该函数禁用旋转。

    self.restrictRotation(restrict: false)

在 viewWillDisappear() 方法中,调用该函数启用旋转。

    self.restrictRotation(restrict: true)

【讨论】:

  • 嘿。我已经尝试过您的解决方案,遗憾的是它不适用于我的情况。没有错误或任何东西。无论代码如何,它都会简单地旋转。
  • 就我而言,它工作正常。哪个是设备操作系统版本。
  • 这是一个 iPad pro,版本 12.3.1。
  • 它在 iPhone 8 12.3.1 中正常工作。我更新答案。请检查一下。在 viewDidload() 中调用该函数
  • 嗯,我还没有运气。我认为可能有一些设置配置导致了这个问题。我在测试应用程序上尝试了您的解决方案并且它有效。我会将其标记为正确答案。
猜你喜欢
  • 2017-03-24
  • 1970-01-01
  • 2014-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多