nickffox / KeyboardObserving

⌨️A Combine-based way to observe and adjust for Keyboard notifications in SwiftUI
MIT License
482 stars 36 forks source link

How to manually show/dismiss the keyboard? #19

Closed SeRG1k17 closed 4 years ago

SeRG1k17 commented 4 years ago

I found a way to hide the keyboard through the window hierarchy:

extension View {
    func endEditing(_ force: Bool) {
        UIApplication.shared.windows.forEach { $0.endEditing(force)}
    }
}

But this is clearly not the best solution. I have not looked at all the code, but it will be sufficient to make this initializer public:

//Keyboard.swift
    public init(animationDuration: TimeInterval, height: CGFloat) {
      self.animationDuration = animationDuration
      self.height = height
    }
    var body: some View {
        KeyboardObservingView {
            NavigationView {
            }
            .onTapGesture {
                self.keyboard.state = Keyboard.State(animationDuration: 0,25, height: 0)
            }
       }
   }
github-actions[bot] commented 4 years ago

Thanks for creating an issue!

nickffox commented 4 years ago

@SeRG1k17 - this framework doesn’t actually show or hide the keyboard, it adjusts your views so that the keyboard doesn’t cover them.

If I’m understanding your question correctly it’s out of scope for this library.

I think instead of using this library to show/hide the keyboard, you’ll want to do something like what you posted where you call beginEditng/endEditing manually.