Using UIImagePickerController in landscape orientation

后端 未结 8 667
暗喜
暗喜 2020-11-28 09:55

I am creating an app which is in landscape mode and I am using UIImagePickerController to take photos using iPhone camera in it and I want to create it in lands

8条回答
  •  暖寄归人
    2020-11-28 10:37

    This works great with Swift 4.0 in iOS 10/11.

    import UIKit
    
    extension UIImagePickerController {
        override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            return .all
        }
    }
    

    Just drop the extension somewhere in your project, no need to subclass anything for it to work.

    If you do need to specify device types, you can add a check like this:

    import UIKit
    
    extension UIImagePickerController {
        override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            return UIDevice.current.userInterfaceIdiom == .phone ? .portrait : .all
        }
    }
    

    This will allow an iPad to freely rotate, but enforces portrait mode on a phone. Just make sure that your app is configured to support these in its info.plist, otherwise you may encounter crashes upon launching the picker.

提交回复
热议问题