2015-10-10 38 views
3

我正在編寫一個僅限LandScape的iPad應用程序,我需要從庫中發送圖片以發送數據庫,但圖像上傳屏幕只能在縱向模式下使用。我如何將其更改爲橫向模式?我讀過一些關於UIPickerControllerDelegate的內容,它不支持橫向模式,但是一些應用程序(如iMessage)已經在使用它。在Swift 2.0中以橫向模式使用UIImagePickerController

這裏是我的代碼:

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

回答

15

它完全支持橫向模式。把這個擴展放在某個地方。最好在一個名爲的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] 

對於斯威夫特3:

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

爲了解決這個問題,我必須啓用肖像模式,但我的應用程序不支持它,因此我無法啓用它。如果縱向模式已啓用,代碼將在橫向模式下工作,但當我禁用縱向模式時,它不起作用。 –

+0

我已添加覆蓋func shouldAutorotate() - >布爾{ 返回false }這對每個視圖控制器來說,它的壞但有用的解決方案。你有其他想法嗎?感謝您的幫助 –

+0

它的工作Charmly – Ben10

相關問題