简体   繁体   English

PHPickerViewController 的取消按钮在 iOS 15 中不起作用

[英]PHPickerViewController's Cancel Button is not working in iOS 15

I am using PHPickerViewController to pick Image for User Profile Picture Purpose in iOS 15. I am using UIKit framework.我正在使用PHPickerViewController在 iOS 15 中为用户个人资料图片目的选择图像。我正在使用 UIKit 框架。 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. Picker 在选择图像和委派结果方面工作正常。 But, when the Cancel button is pressed, nothing happens and the Picker is not dismissed as expected.但是,当按下Cancel按钮时,什么都没有发生,并且 Picker 没有按预期解除。

How to dismiss the PHPickerViewController instance when its own Cancel button is pressed ?PHPickerViewController自己的Cancel按钮被按下时,如何关闭它?

Edit:编辑:

The implementation of PHPickerViewControllerDelegate Method is as follows: PHPickerViewControllerDelegate方法的实现如下:

func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult])
    {
        results.first?.itemProvider.loadObject(ofClass: UIImage.self) { [unowned self] reading , error in
            guard let image = reading as? UIImage, error == nil else
            {
                DispatchQueue.main.async {
                    picker.dismiss(animated: true)
                    self.profilePictureHasError = true
                    self.toggleDoneButtonEnabled()
                }
                return
            }
            self.profilePictureHasError = false
            DispatchQueue.main.async {
                picker.dismiss(animated: true)
                self.profilePictureHasChanged = self.userProfilePicture != image
                if self.profilePictureHasChanged
                {
                    self.profilePictureView.image = image
                    self.toggleDoneButtonEnabled()
                }
            }
        }
    }

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.您需要在用户完成选择点击取消按钮时调用的picker(_:didFinishPicking:)委托方法中自行关闭选择器。

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

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)
}

Your current delegate code only calls picker.dismiss when the results array is non-empty (ie when the user has selected images).您当前的委托代码仅在结果数组非空时(即当用户选择图像时)调用picker.dismiss When the cancel button is tapped, the delegate method is called with an empty results array.当点击取消按钮时,将使用空的结果数组调用委托方法。

Fix the issue by adding the following to the top of the code in your delegate method:通过在委托方法的代码顶部添加以下内容来解决此问题:

if results.isEmpty {
    picker.dismiss(animated: true)
    return
}

you just wrap it out in an objc func for making cancel button works您只需将其包装在 objc 函数中即可使取消按钮起作用

    @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在任何地方调用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 键盘的地球按钮()消失了,iOS 15 - Keyboard's globe button (🌐) was disappeared, iOS 15 iOS ShareKit取消按钮不适用于Xcode 4.2 - iOS ShareKit Cancel button is not working with Xcode 4.2 “取消”按钮 (UIBarButtonItem) 在 iOS 视图 controller 中不起作用 - "Cancel" button (UIBarButtonItem) not working in iOS view controller UISearchBar的取消和清除按钮在iOS 7中不起作用 - UISearchBar's Cancel and Clear Buttons Not Working in iOS 7 当用户在 ios 13.0 中单击登录按钮时,Facebook 登录总是被取消,但它在 ios 12.0 或更小的 swift 中完全正常工作 - Facebook login is always cancel when user click on login button in ios 13.0 but it's totally working fine in ios 12.0 or smaller in swift button.setImage(nil, for: .normal) 在 iOS 中不起作用 15 - button.setImage(nil, for: .normal) not working in iOS 15 iOS LAPolicyDeviceOwnerAuthentication“取消”按钮 - iOS LAPolicyDeviceOwnerAuthentication “Cancel” Button setNavigationBarHidden 不适用于 iOS 15 - setNavigationBarHidden not working on iOS 15 无法在iO中翻译QComboBox的“取消”和“完成”按钮 - Can't translate QComboBox's “Cancel” and “Done” button in iOs 邮件界面取消按钮不起作用的ios-MFMailComposeViewController类 - mail interface cancel button not working ios - MFMailComposeViewController class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM