1

I am having this weird and odd behaviour with the PHPickerViewController. I am using SwiftUI and I have implemented the PHPickerViewController as a UIViewControllerRepresentable and everything seems to be working 100% fine expect when the user taps the "Show Selected (x)" button, the user cannot dismiss the screen. There does seem to be an invisible button the right of the Title in the navigation bar.

struct PhotoPickerView: UIViewControllerRepresentable {
    
    //MARK: - Properties
    @StateObject var messageFormViewModel: MessageFormViewModel

    
    //MARK: - View
    func makeUIViewController(context: Context) -> PHPickerViewController {
        var assetIdentifiers = [String]()
        let photoLibrary = PHPhotoLibrary.shared()
        var config = PHPickerConfiguration(photoLibrary: photoLibrary)
        config.filter = .images
        config.selectionLimit = getAttachmentLimit()
        config.selection = .ordered
        for asset in messageFormViewModel.photoPickerMedia {
            assetIdentifiers.append(asset.id)
        }
        config.preselectedAssetIdentifiers = assetIdentifiers
        let picker = PHPickerViewController(configuration: config)
        picker.delegate = context.coordinator
        return picker
    }

    func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) {

    }
    
    private func getAttachmentLimit() -> Int {
        guard let attachmentLimit = ApplicationSettingsManager.shared.settings?.limits.maxFileAttachments else {
            return 0
        }
        
        return attachmentLimit
    }

    
    //MARK: - Coordinator
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    class Coordinator: NSObject, PHPickerViewControllerDelegate {
        let parent: PhotoPickerView

        init(_ parent: PhotoPickerView) {
            self.parent = parent
        }

        func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
            picker.dismiss(animated: true)
            
            for result in results {
                guard let id = result.assetIdentifier else {
                    continue
                }
                
                let provider = result.itemProvider
                if provider.canLoadObject(ofClass: UIImage.self) {
                    provider.loadObject(ofClass: UIImage.self) { image, _ in
                        if let image = image as? UIImage  {
                            
                            let viewModel = PhotoPickerViewModel(id: id,
                                                                 jpgData: image.jpegData(compressionQuality: 1.0)!,
                                                                 image: image,
                                                                 size: image.size)
                            DispatchQueue.main.async() {
                                self.parent.messageFormViewModel.photoPickerMedia.append(viewModel)
                            }
                        }
                    }
                }
            }
        }
    }
}

enter image description here

BrettS
  • 295
  • 6
  • 14
  • Which OS and device are you building on? There should be a fine button in top right corner if I’m not mistaken. – valosip Jul 28 '23 at 06:06
  • That is what I was thinking @valosip. Building and testing on iOS16 on iPhone 12 and iPhone X – BrettS Jul 28 '23 at 09:10

0 Answers0