我正在编写一个仅限 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)
}
}
最佳答案
它绝对支持横向模式。把这个扩展放在某个地方。最好在名为 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
}
}
关于ios - 在 Swift 2.0 中以横向模式使用 UIImagePickerController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33058691/