Matthew T
Matthew T

Reputation: 93

UIImagePickerController landscape mode shifts screen

I am trying to do a simple SwiftUI application that opens the camera or allows the user to pick a photo from the library. Currently, when I rotate the phone to landscape mode in the opened camera view, the whole interface shifts outside the screen for some reason.

The code that calls the UIImagePickerController:

struct CaptureImageView {
@Binding var isShown: Bool
@Binding var image: UIImage?
@Binding var sourceType: UIImagePickerController.SourceType
@Binding var imageSelected: Bool

func makeCoordinator() -> Coordinator {
    return Coordinator(isShown: $isShown, image: $image, imageSelected: $imageSelected)
}}

extension CaptureImageView: UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<CaptureImageView>) -> UIImagePickerController {
    let picker = UIImagePickerController()
    picker.sourceType = sourceType
    picker.delegate = context.coordinator
    picker.allowsEditing = true
    picker.setEditing(true, animated: true)
    if (picker.sourceType == .camera) {
        picker.cameraCaptureMode = .photo
    }
    return picker
}

func updateUIViewController(_ uiViewController: UIImagePickerController,
                            context: UIViewControllerRepresentableContext<CaptureImageView>) {
    
}}

How it looks like when I rotate the screen:

rotated screen

Is there a proper way to make it adjust the view on orientation changes?

Upvotes: 0

Views: 259

Answers (1)

Sandoze
Sandoze

Reputation: 470

The most important first step is to read the documentation for UIImagePickerController provided by Apple displayed prominently.

The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and doesn’t support subclassing. The view hierarchy for this class is private and must not be modified...

Just because this view might rotate correctly in UIKit does not mean it will be supported in the future or behave as intended.

Camera capture is handled by AVKit and a video/image capture can be created using this framework. Rotation is handled through your implementation.

I also strongly suggest for anyone needing a more immediate or less complex solution look at Mijick Camera. This framework is highly customizable and easy to work with.

Upvotes: 0

Related Questions