Sunday, March 17, 2024
HomeiOS Developmentios - Drive resign the primary responder in SwiftUI

ios – Drive resign the primary responder in SwiftUI


I at the moment use a QLPreviewController in a SwiftUI app with the assistance of UIViewControllerRepresentable to show a PDF.

This works effectively because the person can work together with the PDF utilizing the keyboard to enter information but additionally utilizing PencilKit

QLPreviewController PencilKit SwiftUI

I used to be questioning if there was a strategy to power resign the primary responder inside the QLPreviewController because the enter may both be a textual content discipline with the keyboard or drawing with the PKToolPicker.

That is what I attempted:

  1. I created this extension to seek out the important thing window primarily based on this reply
extension UIApplication {
    
    var keyWindow: UIWindow? {
        // Get linked scenes
        return self.connectedScenes
            // Preserve solely lively scenes, onscreen and visual to the person
            .filter { $0.activationState == .foregroundActive }
            // Preserve solely the primary `UIWindowScene`
            .first(the place: { $0 is UIWindowScene })
            // Get its related home windows
            .flatMap({ $0 as? UIWindowScene })?.home windows
            // Lastly, hold solely the important thing window
            .first(the place: .isKeyWindow)
    }
    
}
  1. Then utilizing this reply, I execute this line of code when tapping on a SwiftUI button:
UIApplication.shared.keyWindow?.endEditing(true)

Nonetheless this does not appear to dismiss the keyboard nor the PKToolPicker

  1. I’ve even tried the beneath which I discovered on one other reply:
extension UIApplication {
    func endEditing() {
        sendAction(#selector(UIResponder.resignFirstResponder),
                   to: nil,
                   from: nil,
                   for: nil)
    }
}

And whereas this labored when on my login type once I wished to dismiss the keyboard, this does not appear to work inside the QLPreviewController

Is there another approach I can power resignFirstResponder or is there one thing much like finish enhancing for SwiftUI ?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments