xcode 在 Swift 2.0 中以横向模式使用 UIImagePickerController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33058691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use UIImagePickerController in landscape mode in Swift 2.0
提问by Berk Kaya
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.
我正在编写一个仅限 LandScape 的 iPad 应用程序,我需要从库中拍照以发送数据库,但图像上传屏幕仅适用于纵向模式。如何将其更改为横向模式?我读过一些关于 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)
}
}
回答by Морт
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. You can also subclass it and override this method to make only a subclass landscape-able:
这使您的应用程序环境中的所有 UIImagePickerControllers 。您还可以将其子类化并覆盖此方法以仅使子类可横向显示:
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
}
}