Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use UIImagePickerController in landscape mode in Swift 2.0

Tags:

xcode

ios

swift

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)
}
}
like image 896
Berk Kaya Avatar asked Oct 10 '15 20:10

Berk Kaya


1 Answers

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]

For Swift 3:

    extension UIImagePickerController
    {
        override open var shouldAutorotate: Bool {
                return true
        }
        override open var supportedInterfaceOrientations : UIInterfaceOrientationMask {
                return .all
        }
}
like image 178
Морт Avatar answered Nov 15 '22 07:11

Морт