6
votes

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.

3
try make something else become the first responder, maybe just the base view of the viewcontroller - Fonix
your keyboard is resigned and only cursor is available - Anbu.Karthik
@Fonix I've already tried to set textfield.superView to firstresponder, but it doesn't help. Another guide tells that I need to set firstresponder to another textfield. But I don't have any other textfield. I only have that one textfield in the screen, with other components, but no other components can become firstresponder besides the textfield. - Chen Li Yong

3 Answers

5
votes

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()
4
votes

I had the same problem ages ago.

I had a screen containing a UITextField, and some other controls, some of which were appearing under the onscreen keyboard (yes, I know now I should've used constraints to make sure the onscreen UIView fitted the visible part of the screen).

The problem in my case was that on a Modal screen, once the onscreen keyboard appears, you can never get rid of it, even if you try using resignFirstResponder. In its wisdom, Apple has deliberately designed it this way, to stop the user being annoyed by the keyboard appearing, and disappearing. Uh-huh.

My solution was simple:

Make your UITextField disabled. Even if it's just for a fraction of a second, then you re-enable it.

This'll make the onscreen keyboard nicely slide away.

1
votes

If you just need to detect when the textfield is touched and not specifically the didBeginEditing, why not just make a subclass of UITextField and override touchesBegan like so

class SpecialTextField: UITextField {
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        print("touched textfield")
    }
}

then you can use that to fire your event via a delegate method, or directly somehow