Home > Software design >  How to show pencil option in UIDocumentInteractionController?
How to show pencil option in UIDocumentInteractionController?

Time:05-27

I am trying to use UIDocumentInteractionController in swiftUI using UIViewControllerRepresentable. My code works fine but I want to add pencil functionality to my app. I wanna be able to draw over image using pencil or finger. When you open an image in files app you get that pencil button on top right corner. I am using UIDocumentInteractionController which only gives done button and share button on the top. I want to know if files app using the same thing for opening images. If Yes, how to enable that pencil button ? and if No, then what is it that files app using to open images ?

Thank You!

UIViewControllerRepresentable

struct UIDocumentInteractionControllerWrapper: UIViewControllerRepresentable {
    private var isActive: Binding<Bool>
    private let docController: UIDocumentInteractionController
    
    private let viewController = UIViewController()
    
    internal init(isActive: Binding<Bool>, url: URL) {
        self.isActive = isActive
        self.docController = UIDocumentInteractionController(url: url)
    }
    
    func makeUIViewController(context: Context) -> UIViewController {
        return viewController
    }
    
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        
        if self.isActive.wrappedValue && docController.delegate == nil {
            docController.delegate = context.coordinator
            self.docController.presentPreview(animated: true)
        }
    }
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }
    
    final class Coordinator: NSObject,UIDocumentInteractionControllerDelegate {
        let parent: UIDocumentInteractionControllerWrapper
        
        init(_ parent: UIDocumentInteractionControllerWrapper) {
            self.parent = parent
        }
        
        func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
            return parent.viewController
        }
        
        func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) {
            controller.delegate = nil
            parent.isActive.wrappedValue = false
        }
    }
}

My app

image

Apple's files app

enter image description here

CodePudding user response:

It is QLPreviewController on second screenshot,

demo

see test module here for how to integrate it.

Also this my post about integration with UINavigationController might be helpful

And this one

  • Related