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

时间:2015-10-10 20:33:14

标签: ios xcode swift

我正在编写仅限LandScape的iPad应用程序,我需要从库中拍照才能发送数据库,但图像上传屏幕仅适用于纵向模式。如何将其更改为横向模式?我读过UIPickerControllerDelegate并不支持横向模式但有些应用程序(例如iMessage)已经在使用它了。

这是我的代码:

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

1 个答案:

答案 0 :(得分:20)

它绝对支持横向模式。把这个扩展放在某个地方。最好在名为UIImagePickerController + SupportedOrientations.swift

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

这使得所有UIImagePickerControllers都在你的应用程序中。您也可以对其进行子类化并覆盖此方法,以便只创建一个子类格式:

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

最后,为了支持所有方向,你可以返回

return [.Landscape, .Portrait]

对于Swift 3:

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