-1

Hi I want to keep the keyboard up while navigation link happens or first dismiss the keyboard when button is tapped and then navigate when keyboard is fully dismissed in SwiftUI? Can someone help me?

user12723399
  • 127
  • 1
  • 1
  • 5

1 Answers1

0

I am not sure what you want to achieve but my experience is that when you have a view which opens a keyboard and that view is dismissed, SwiftUi crashes. Therefore i make sure that the keyboard is always closed when moving to another view.

Make an extension to close the keyboard:

    extension UIApplication {
        func endEditing() {
            sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
        }
    }

in SwiftUI:

        Button(action: {
          UIApplication.shared.endEditing()
          withAnimation(.easeInOut(duration: 0.3)) {
            // do something
          }
        }) {
          Text("Button")

        }

...
Peter Pohlmann
  • 1,478
  • 15
  • 30