0
votes

How I could change the device orientation to Portrait with code?

In order to open the camera and library in portrait from the view controller.

I would like to include in this button:

@IBAction func openCamera(sender: AnyObject) { ///accion del boton Camara

    // Create image picker controller


    let image = UIImagePickerController()

    image.delegate = self

    image.sourceType = UIImagePickerControllerSourceType.Camera
    image.cameraDevice  = UIImagePickerControllerCameraDevice.Front
    self.presentViewController(image, animated: true, completion: nil)
}

I got this error:

Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [PLUICameraViewController shouldAutorotate] is returning YES' *** First throw call stack: (0x18150ee38 0x180b73f80 0x18150ed80 0x1866d1af0 0x1866db150 0x1866db0c8 0x1866d9bb0 0x186653910 0x1866536ac 0x1866536ac 0x1866536ac 0x186652c40 0x181e40d50 0x186652ac4 0x186660578 0x18675a11c 0x186759a40 0x1869b8740 0x18690afd8 0x186918990 0x18664a4a4 0x1814c47b0 0x1814c2554 0x1814c2984 0x1813ecd10 0x182cd4088 0x1866c1f70 0x1000675ac 0x180f8a8b8) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

When I use

> presentViewController(cameraViewController, animated: true,
> completion: nil)
>     }
>     */
>     
>         // Create image picker controller
>         
>         
>         let image = UIImagePickerController()
>         
>         image.delegate = self
>         
>         image.sourceType = UIImagePickerControllerSourceType.Camera
>         image.cameraDevice  = UIImagePickerControllerCameraDevice.Front
>         self.presentViewController(image, animated: true, completion: nil)
>     }
3
forcing the device orientation is not the way to go. the problem is also not the device but the interface orientation and that you specify in your AppDelegate or your app's plist. Do you forbid portrait? then allow it :PDaij-Djan
that does mean that your viewControllers must support it. but it will enable it for VCs that want itDaij-Djan

3 Answers

0
votes

In order to change orientation programmatically, use this :

let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
0
votes

forcing the device orientation is not the way to go. the problem is also not the device but the interface orientation :) there is a distinction to read about

but to solve the issue, just allow portrait mode in your app and that you specify in your AppDelegate or your app's plist. :)

//allow rotation for app
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
    return UIInterfaceOrientationMaskAll;
}

... you can THEN forbid it for certain VCs instead of for everything ...

//for a vc
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeLeft;
}
- (BOOL)shouldAutorotate {
    return YES;
}
0
votes

Use setter method for setting up orientation

  private var orientations = UIInterfaceOrientationMask.landscapeLeft
    override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
        get { return self.orientations }
        set { self.orientations = newValue }
    }