简体   繁体   English

在Swift 2.0中以横向模式使用UIImagePickerController

[英]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. 我正在编写仅限LandScape的iPad应用程序,我需要从库中拍照才能发送数据库,但图像上传屏幕仅适用于纵向模式。 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. 我读过一些关于UIPickerControllerDelegate的东西不支持横向模式,但是一些应用程序(例如iMessage)已经在使用它了。

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)
}
}

It absolutely supports landscape mode. 它绝对支持横向模式。 Put this extension somewhere. 把这个扩展放在某个地方。 Best in a file named UIImagePickerController+SupportedOrientations.swift 最好在名为UIImagePickerController + SupportedOrientations.swift的文件中

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

This makes all UIImagePickerControllers in your app landscape. 这使得所有UIImagePickerControllers都在您的应用程序环境中。 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: 对于Swift 3:

    extension UIImagePickerController
    {
        override open var shouldAutorotate: Bool {
                return true
        }
        override open var supportedInterfaceOrientations : UIInterfaceOrientationMask {
                return .all
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM