David
David

Reputation: 317

Dismiss keyboard in ios17

I am having trouble getting the keyboard to dismiss. When nil it should dismiss but it's not working so I added the dismiss but that's also not working. The keyboard flashes but stays. Any suggestions on what I'm missing here. I'm testing on ios17 both in the simulator and on phone and get the same problem:

Here is the code I'm trying:

import SwiftUI

struct ContentView: View {
    @Environment(\.colorScheme) var colorScheme
    @FocusState private var focusedField: Field?
    @State private var lastname: String = ""

    enum Field: Hashable {
        case lastName
    }

    var body: some View {
        VStack {
            HStack {
                TextField("Last name", text: $lastname)
                    .disableAutocorrection(true)
                    .foregroundColor(Color.primary)
                    .autocapitalization(.words)
                    .textContentType(.familyName)
                    .focused($focusedField, equals: .lastName)
                    .submitLabel(.done)
                    .onSubmit {
                        focusedField = nil
                        dismissKeyboard()
                    }
                    .onTapGesture {
                        focusedField = .lastName
                    }
                Spacer()
            }
            .frame(height: 40)
            .overlay(
                Rectangle() // This creates the underline effect
                    .frame(height: 0.75)
                    .foregroundColor(colorScheme == .dark ? .white : .black)
                    .padding(.top, 30)
            )
            .padding()
        }
    }

    private func dismissKeyboard() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Upvotes: 1

Views: 66

Answers (1)

Andrei G.
Andrei G.

Reputation: 1557

If you're testing on iOS 17, you don't need the dismissKeyboard() function . That function is only if you need support for iOS 13 or 14.

To make it work the way you go about it, change your @FocusState var to be of Bool type, then simply set it to false when you want to hide the keyboard.

In the example below, I added a keyboard icon to dismiss the keyboard in a couple places, as a suggestion.

import SwiftUI

struct ContentView: View {
    @Environment(\.colorScheme) var colorScheme
    @FocusState private var focusedField: Bool
    @State private var lastname: String = ""
    
    enum Field: Hashable {
        case lastName
    }
    
    var body: some View {
        VStack {
            HStack {
                TextField("Last name", text: $lastname)
                    .disableAutocorrection(true)
                    .foregroundColor(Color.primary)
                    .autocapitalization(.words)
                    .textContentType(.familyName)
                    .focused($focusedField)
                    .submitLabel(.done)
                    .onSubmit {
                        focusedField = false
                    }
                    .onTapGesture {
                        focusedField = false
                    }
                Spacer()
                if focusedField == true {
                    Button("\(Image(systemName: "keyboard.chevron.compact.down"))"){
                        focusedField = false
                    }
                }
            }
            .frame(height: 40)
            .overlay(
                Rectangle() // This creates the underline effect
                    .frame(height: 0.75)
                    .foregroundColor(colorScheme == .dark ? .white : .black)
                    .padding(.top, 30)
            )
            .padding()
            .toolbar {
                ToolbarItem(placement: .keyboard) {
                    Button("\(Image(systemName: "keyboard.chevron.compact.down"))"){
                        focusedField = false
                        }
                }
            }
        }
    }

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Upvotes: 0

Related Questions