I have seen this question asked million times everywhere, but there's no single answer that has been working for me.
I need to remove focus from a UITextField. That means I need the cursor to go away (and the keyboard dismissed).
The solution I've seen on the internet is either [textfield resignFirstResponder]
or [textfield endEditing:YES]
, which is able to hide the keyboard, but does not remove focus from UITextField (i.e. the cursor still blinking happily inside the UITextField, although the keyboard is dismissed).
The thing with this is I need to get event when the user tap into the UITextField and the event didBeginEditing is fired. I'm doing something each time that event is fired, or more generally, each time the user tap on the UITextField. But if the focus isn't removed entirely from the UITextField, the event can't be fired again even after I called resignFirstResponder
or endEditing:YES
.
How can I achieve this? Thanks.
If you're supporting only iOS 15 and later, you can activate and dismiss the keyboard for a text field by focusing and unfocusing it. In its simplest form, this is done using the @FocusState property wrapper and the focusable() modifier – the first stores a Boolean that tracks whether the second is currently focused.
you can make the cusor color as clear color
textfeild.tintColor = UIColor.clearColor()
if you just want to dismiss the keyboard then
textfeild.resignFirstResponder()
will dismiss the keyboard and when you want focus again use
textfeild.becomeFirstResponder()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With