Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove focus from UITextField?

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.

like image 474
Chen Li Yong Avatar asked Mar 24 '16 04:03

Chen Li Yong


People also ask

How do I Unfocus a textfield Swiftui?

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.


1 Answers

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()
like image 50
IOS DEV Avatar answered Nov 15 '22 05:11

IOS DEV