31382

Swift manually rotate view controller

Question:

So I'm working on an iOS 10 app using Swift 3 and Xcode 8.3.3. In my app I have to take successively 8 pictures with indicators on the camera.

So I'm using AVCaptureVideoPreviewLayer for the custom camera and I need to set the view displaying the camera on landscape mode. I did it using this extension that I found on stackoverflow while searching for the problem.

struct AppUtility { static func lockOrientation(_ orientation: UIInterfaceOrientationMask) { if let delegate = UIApplication.shared.delegate as? AppDelegate { delegate.orientationLock = orientation } } /// OPTIONAL Added method to adjust lock and rotate to the desired orientation static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) { self.lockOrientation(orientation) UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation") } }

and in the viewDidLoad() of the customCameraView, I'm setting the locking the orientation to .landscapeLeft :

AppUtility.lockOrientation(.landscapeLeft)

before leaving the view I'm setting this in my viewWillDissapear method in order to unlock the orientation:

AppUtility.lockOrientation(.all)

The problem is that landscape mode in the customCameraView works only when the auto-rotation of the device is enabled (not locked) and when I get back to the previous view controller, it is initially displayed in landscapeLeft so I have to turn the device to put it portrait mode. this view is locked in portrait method using the AppUtility extension.

So I thought about always activate the auto-rotation by overriding the shouldAutoRotate var but it didn't work when the device auto-rotation is locked.

Then I thought about making sure that the auto-rotation is enabled before opening the camera but 100 of people on stackoverflow are saying that this is not possible.

So the perfect solution for me would be to always make the customCameraView in landscapeLeft mode and the previous view in portrait no matter if the rotate is activated or not.

I'm struggling on this bug for days now and a little help would be great.

Answer1:

iOS encourages developers to support both portrait and landscape, so it can be difficult to restrict one part of the app to be landscape while the other is portrait.

One option that works pretty well is to restrict the entire app to portrait mode in the project settings, and apply a rotation transform to any view controllers that need to be landscape. This way, the view will look and behave like it is landscape, and you have full control over the rotation of your views.

Answer2:

Can you apply below code on the required customCameraView Controller :

override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return UIInterfaceOrientationMask.landscapeLeft }

Recommend

  • IISExpress application pool recycle
  • forEach over es6 Map in JSX
  • How does the compiler internally solve the diamond problem in C++?
  • How to Upload Large Video FIle on server?
  • Vertica - Is there LATERAL VIEW functionality?
  • Traversing arbitrarily large binary tree inorder
  • Modifying the Javascript Navigator object with Selenium
  • Material Transitions in pre lollipop apps
  • How to create google chrome like install for my app? [duplicate]
  • Parse and upload a csv file in D3.js V5
  • Inheritance and lazy loading in NHibernate
  • type “e” does not exist , Redshift through Postgresql connector in php codeigniter
  • undefined is not an object(evaluating '_this2.props.navigation.navigate') Alert.alert
  • how to refresh alertdialog in flutter
  • Why is the line of wss-plot (for optimizing the cluster analysis) looks so fluctuated?
  • CMake with regarding generated files
  • How to handle asynchronous error in Node.js
  • sending emails in python weird behaviour
  • On Woocommerce order complete, activate woosensei course
  • Java Processbuilder Stream to Python-Script
  • How do I remove line feed characters when selecting data from SQL Server?
  • SQLITE multiple table join with a condition
  • Bootstrap 3 Validation
  • analytics.js event not working properly
  • Decimal to ASCII Conversion [closed]
  • Custom Data Generator for Keras LSTM with TimeSeriesGenerator
  • Ways of filling 10 places with number from [1..10] such that digit at ith place has value atmost 1 m
  • Problems with special characters in php soap client
  • jQuery - Prevent activation on first click
  • How to get a time and Date Separately?
  • Does hibernate load two seprate copies of same instance if they are loaded twice from database?
  • Facebook Error (#200) The user hasn't authorized the application to perform this action (PHP)
  • Autocomplete source from project settings
  • How to have non scrolling content in a UIScrollView
  • How to specify generic type when the type is only known at runtime?
  • SpringBoot: Bypass OncePerRequestFilter filters
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • read part of h5 dataset python
  • Android Library Projects on Windows and Mac
  • convert json to excel in java