Saturday, March 16, 2024
HomeiOS Developmentios - Get notified when PKToolPicker is seen in QLPreviewController

ios – Get notified when PKToolPicker is seen in QLPreviewController


I at the moment present a QLPreviewController in a SwiftUI app utilizing a UIViewControllerRepresentable – right here is the code:

struct QLPDFPreviewController: UIViewControllerRepresentable {
    let url: URL?
    
    func makeUIViewController(context: Context) -> UINavigationController {
        let controller = QLPreviewController()
        controller.dataSource = context.coordinator
        controller.delegate = context.coordinator
        let navigationController = UINavigationController(rootViewController: controller)
        return navigationController
    }
    
    func updateUIViewController(_ uiViewController: UINavigationController,
                                context: Context) { }
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(mum or dad: self)
    }
    
    class Coordinator: NSObject, QLPreviewControllerDataSource, QLPreviewControllerDelegate {
        let mum or dad: QLPDFPreviewController
        
        init(mum or dad: QLPDFPreviewController) {
            self.mum or dad = mum or dad
        }
        
        func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
            return 1
        }
        
        func previewController(_ controller: QLPreviewController,
                               previewItemAt index: Int) -> QLPreviewItem {
            guard let url = mum or dad.url else { return NSURL(string: "")! }
            return url as NSURL
        }
        
        func previewController(_ controller: QLPreviewController,
                               editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
            .updateContents
        }
    }
}

I exploit this to show a PDF and utilizing the QLPreviewController the person can enter textual content into the PDF and draw on the PDF.

The whole lot works superb by way of performance. I now have a requirement to do some processing when the keyboard has opened and the PKToolPicker.

Listening for keyboard occasions is ok as I may use the UIResponder.keyboardWillShowNotification notification, nonetheless, I couldn’t discover one thing just like get notified when the PKToolPicker is open.

I did come throughout func toolPickerVisibilityDidChange(PKToolPicker) within the documentation, nonetheless, it looks as if we will solely use this if we initialize our personal PKToolPicker.

Is there any generic method to pay attention as to whether a PKToolPicker is seen just like one thing we do for the keyboard ?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments