输入附件视图在图像选择器取消后消失 [Swift]
Input Accessory View disappear after Image Picker is cancelled [Swift]
我有一个非常标准的 inputAccessoryView
,带有图像和相册按钮。但我发现如果在显示键盘时启动相册(换句话说,textView
是第一响应者),当相册被关闭时 inputAccessoryView
消失。如何让它一直停留在屏幕底部?
我的实现是通用的:
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)
}
}
这是演示问题的 gif。注意 inputAccessoryView
最后消失了。非常感谢!
尝试在关闭图像选择器后添加 self.inputAccessoryView.becomeFirstResponder()。
我解决了。事实证明,在显示图像选择器之前,我需要请求 textView 到 resignFirstResponder()
。另外,添加以下代码。它处理用户取消拾取操作的情况。
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
根据 Apple 文档:
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.
dismissModalViewControllerAnimated:
方法已弃用,应改用 dismissViewControllerAnimated:completion:
。
我有一个非常标准的 inputAccessoryView
,带有图像和相册按钮。但我发现如果在显示键盘时启动相册(换句话说,textView
是第一响应者),当相册被关闭时 inputAccessoryView
消失。如何让它一直停留在屏幕底部?
我的实现是通用的:
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)
}
}
这是演示问题的 gif。注意 inputAccessoryView
最后消失了。非常感谢!
尝试在关闭图像选择器后添加 self.inputAccessoryView.becomeFirstResponder()。
我解决了。事实证明,在显示图像选择器之前,我需要请求 textView 到 resignFirstResponder()
。另外,添加以下代码。它处理用户取消拾取操作的情况。
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
根据 Apple 文档:
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.
dismissModalViewControllerAnimated:
方法已弃用,应改用 dismissViewControllerAnimated:completion:
。