Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to lock rotation for one view controller in IOS10

Background

I have an app that uses AVFoundation in order to have a custom camera. This happens in the OCRViewController. When I take a picture I send the captured picture to a different view ImagePreviewViewController.

I am using Xcode 10.2.1 (10E1001) with Swift 5

The Goal

What I would like to achieve is to lock the orientation of the ImagePreviewViewController to the original orientation of the image. I already know how to get the orientation of the image but I am not able to lock the orientation of the view.

I get the image rotation as such: let imageOri = capturedImage?.imageOrientation

What did I try?

I tried the accepted answers at and several other sources:

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

Reading the documentation at https://developer.apple.com/documentation/uikit/uiviewcontroller#//apple_ref/occ/instm/UIViewController/supportedInterfaceOrientations under Handling View Rotation the following is stated:

I also tried the many suggested solutions while writing this query, however, the majority appears to use the following approach (or a variation of it), and it does not work for me.

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

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

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

As of iOS 8, all rotation-related methods are deprecated. Instead, rotations are treated as a change in the size of the view controller’s view and are therefore reported using the viewWillTransition(to:with:) method.

However, I am not sure how to progress from here.

Interesting code snippets

The following method is in my OCRViewController, here I instantiate the ImagePreviewViewController and attach the captured image.

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

Using the below override function inside my ImagePreviewViewController I am able to detect the orientation of the view controller.

override func viewDidAppear(_ animated: Bool) {
    if UIDevice.current.orientation.isLandscape {
        print("Landscape")
    } else {
        print("Portrait")
    }
}
like image 944
Spiderixx Avatar asked Nov 07 '22 15:11

Spiderixx


1 Answers

To restrict the rotation of one screen, use this.

In AppDelegate

var restrictRotation = Bool()

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

In your viewcontroller add the function,

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

In the ViewDidload() method, call the function to disable rotation.

    self.restrictRotation(restrict: false)

in viewWillDisappear() method, call the function to enable rotation.

    self.restrictRotation(restrict: true)
like image 173
Vinu Jacob Avatar answered Nov 15 '22 08:11

Vinu Jacob