0

I try to show the text field keyboard as soon as the view appears and dismiss the keyboard when tap on the keyboard "return" key, the first part of the problem is solved by the code example bellow, But that make the keyboard "return" key doesn't work, Did any one can help to achieve my to show and dismiss the textfiled keyboard as I need.

import SwiftUI

struct MyTextField: UIViewRepresentable {
    typealias UIViewType = UITextField
    
    @Binding var becomeFirstResponder: Bool
    
    func makeUIView(context: Context) -> UITextField {
        let textField = UITextField()
        return textField
    }
    
    func updateUIView(_ textField: UITextField, context: Context) {
        if self.becomeFirstResponder {
            DispatchQueue.main.async {
                textField.becomeFirstResponder()
                self.becomeFirstResponder = false
            }
        }
    }
}

struct TextFieldFirstResponder: View {
    
    @State private var becomeFirstResponder = false
    
    var body: some View {
        
        MyTextField(becomeFirstResponder: self.$becomeFirstResponder)
            .onAppear {
                self.becomeFirstResponder = true
            }
    }
}

1 Answer 1

2

Use textFieldShouldReturn delegate method. For this make Coordinator for UIViewRepresentable.

struct MyTextField: UIViewRepresentable {
    typealias UIViewType = UITextField
    
    @Binding var becomeFirstResponder: Bool
    
    func makeUIView(context: Context) -> UITextField {
        let textField = UITextField()
        textField.delegate = context.coordinator
        return textField
    }
    
    func updateUIView(_ textField: UITextField, context: Context) {
        if self.becomeFirstResponder {
            DispatchQueue.main.async {
                textField.becomeFirstResponder()
                self.becomeFirstResponder = false
            }
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(parent: self)
    }

    class Coordinator: NSObject, UITextFieldDelegate {
        var parent: MyTextField
        
        init(parent: MyTextField) {
            self.parent = parent
        }
        
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            textField.resignFirstResponder()
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the answer, Can you give me a simple code example for how I can use this in the SwiftUI view, to show and hide the keyboard.
Just replace this class with your and your second part problem is solved (Dismiss keyboard on "return" key)
I have just added the text field delegate method and I used textFieldShouldReturn delegate method which is a responsible "return" key action.
Thank you very much, The problem is solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.