Home > Blockchain >  PHPickerViewController's Cancel Button is not working in iOS 15
PHPickerViewController's Cancel Button is not working in iOS 15

Time:07-14

I am using PHPickerViewController to pick Image for User Profile Picture Purpose in iOS 15. I am using UIKit framework. I have the following code:

var pickerConfig = PHPickerConfiguration(photoLibrary: .shared())
pickerConfig.selectionLimit = 1
pickerConfig.filter = .images
let pickerView = PHPickerViewController(configuration: pickerConfig)
pickerView.delegate = self
self.present(pickerView, animated: true)

The Picker is working properly for selecting images and delegating the results. But, when the Cancel button is pressed, nothing happens and the Picker is not dismissed as expected.

How to dismiss the PHPickerViewController instance when its own Cancel button is pressed ?

CodePudding user response:

you just wrap it out in an objc func for making cancel button works

    @objc
    func didOpenPhotos() {
        lazy var pickerConfig = PHPickerConfiguration()
        pickerConfig.filter = .images
        pickerConfig.selectionLimit = 1
        let pickerView = PHPickerViewController(configuration: pickerConfig)
        pickerView.delegate = self
        self.present(pickerView, animated: true)
    }

call it anywhere

CodePudding user response:

You need to dismiss the picker yourself in the picker(_:didFinishPicking:) delegate method which is called when the user completes a selection or when they tap the cancel button.

From the Apple docs for picker(_:didFinishPicking:):

The system doesn’t automatically dismiss the picker after calling this method.

For example:

func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
    // Do something with the results here
    picker.dismiss(animated: true)
}
  • Related