Thursday, November 12, 2026
HomeiOS Developmentios - The right way to dismiss keyboard utilizing FocusState for TextField...

ios – The right way to dismiss keyboard utilizing FocusState for TextField and TextEditor in SwiftUI?

[ad_1]

I’m utilizing FocusState to edit a TextField after which dismiss the keyboard with a ‘accomplished’ submit label on the keyboard.

Nevertheless, the .targeted() modifier would not appear to work with a TextEditor on the identical display screen and due to this fact there is no such thing as a ‘accomplished’ button to dismiss the keyboard after typing into the TextEditor.

How can I create a single accomplished button that dismisses the keyboard for each the targeted textual content subject and the textual content editor?

That’s, I’m on the lookout for one thing like this:

struct ContentView: View {
  @FocusState var focusedInput: Enter?
  @State personal var fieldText: String = "Textual content subject"
  @State personal var editorText: String = "Textual content editor"
  
    var physique: some View {
      VStack {
        TextField("", textual content: $fieldText)
          .targeted($focusedInput, equals: .subject)
          .submitLabel(.accomplished)
        
        TextEditor(textual content: $editorText)
          .targeted($focusedInput, equals: .editor)
          .submitLabel(.accomplished)
      }
    }
}

enum Enter {
  case subject
  case editor
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments