iosswiftuiimagepickercontrollerinputaccessoryview

Input Accessory View disappear after Image Picker is cancelled [Swift]


I have a pretty standard inputAccessoryView with image and album buttons. But I found that if album is launched when keyboard is shown (in other words, textView is the first responder), inputAccessoryView disappears when photo album is dismissed. How to make it stay at the bottom of the screen?

My implementation is generic:

let inputContainerView: UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 40)

override var inputAccessoryView: UIView? {
    return inputContainerView
}

lazy var imagePicker: UIImagePickerController = {
    let picker = UIImagePickerController()
    picker.delegate = self
    return picker
}()

// The function that is hooked up to the album button
func handleAlbum() {
    if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
        imagePicker.sourceType = .photoLibrary
        imagePicker.allowsEditing = false
        present(imagePicker, animated: true, completion: nil)
    }
}

Here is a gif demonstrating the problem. Notice the inputAccessoryView disappeared at the end. Thank you so much!

enter image description here


Solution

  • I solved it. It turns out I need to ask textView to resignFirstResponder() before presenting the image picker. Also, add the following code. It handles the case when user cancelled the pick operation.

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
    

    Per Apple documentation:

    Your delegate’s implementation of this method should dismiss the picker view by calling the dismissModalViewControllerAnimated: method of the parent view controller.

    Implementation of this method is optional, but expected.

    The dismissModalViewControllerAnimated: method is deprecated and should use dismissViewControllerAnimated:completion: instead.