Berk Kaya Berk Kaya - 6 months ago 485
Swift Question

Use UIImagePickerController in landscape mode in Swift 2.0

I am coding a LandScape only iPad application and i need to take pictures from library to send a database but image upload screen only works on Portrait mode. How do i change it to landscape mode? I've read something about UIPickerControllerDelegate doesn't support the Landscape mode but some of the apps ( such as iMessage ) is already using this.

here is my code:

class signUpViewController: UIViewController,UIPickerViewDataSource, UIPickerViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate {


func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {

print("Image Selected")

self.dismissViewControllerAnimated(true, completion: nil)

profileImageView.image = image

}



@IBAction func importImage(sender: AnyObject) {

var image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false

self.presentViewController(image, animated: true, completion: nil)
}
}

Answer

It absolutely supports landscape mode. Put this extension somewhere. Best in a file named UIImagePickerController+SupportedOrientations.swift

extension UIImagePickerController
{
    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .Landscape
    }
}

This makes all UIImagePickerControllers in your app landscape. You can also subclass it and override this method to make only a subclass landscape-able:

class LandscapePickerController: UIImagePickerController
{
    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .Landscape
    }
}

Finally, to support all orientations you can return

return [.Landscape, .Portrait]