Using UIViewRepresentable for a TextField to hide the keyboard also hides keyboard outside my app in general iOS

118 Views Asked by At

I have created my own custom keyboard in my iOS swift app, and it works very good. But because i'm using a custom keyboard, i wanted to hide the normal iOS keyboard. I found a solution for this, by using UIViewRepresentable for a TextField. And, it worked very well, until i found out, that I actually also was hiding the iOS keyboard outside of my app as well (how is that even allowed?). So after I have used my app, and then going into ex. Notes or Messages on my iPhone, the keyboard no long appears when i click the text, as it normally does.

If I wait 2-3 minutes, it normally goes back to normal again, and I can again get the keyboard shown in Notes.

I have just updated iOS to 16.7 to see if it was a bug in previous iOS, but that didn't help anything.

I have googled a lot, but can't find a solution, or even others having same issues.

Does anybody know how to correct this? It's not only on my iPhone, also on the tester's iPhone.

This is the usage:

HideKeyboardTextField(text: $tempText)
.background(Color.white)
.frame(width: 200, height: 20)
.focused($textfieldFocused)
.onReceive(Just(tempText)) { text in
  checkText(skipValidation: false)
}

This is the definition:

struct HideKeyboardTextField: UIViewRepresentable {
    @Binding var text: String
    
    func makeUIView(context: UIViewRepresentableContext<HideKeyboardTextField>) -> UITextView {
        let textField = UITextView(frame: .zero)
        textField.inputView = UIView()
        textField.inputAccessoryView = UIView()
        textField.delegate = context.coordinator
        return textField
    }

    func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<HideKeyboardTextField>) {
        uiView.text = text
    }
    
    
    func makeCoordinator() -> HideKeyboardTextField.Coordinator {
        Coordinator(parent: self)
    }

    class Coordinator: NSObject, UITextViewDelegate {
        var parent: HideKeyboardTextField

        init(parent: HideKeyboardTextField) {
            self.parent = parent
        }

        func textViewDidChangeSelection(_ textField: UITextView) {
            DispatchQueue.main.async {
                self.parent.text = textField.text ?? ""
            }
        }
    }
}

I have tried google for a solution, but can't find anobody with the same issue.

0

There are 0 best solutions below