Chen Li Yong
Chen Li Yong

Reputation: 6087

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.

Upvotes: 4

Views: 21867

Answers (5)

Farahad Zama
Farahad Zama

Reputation: 1

Mike Gledhill's answer above worked brilliantly for me. The code I used in SwiftUI to temporarily disable the text field and get rid of the keyboard was this:

@State private var isTextFieldDisabled = false  // New state variable for text field
 ...
TextField("Enter command here", text: $commandText)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .padding(.bottom, 20)
                .disabled(isTextFieldDisabled)  // Disable based on state variable
...
Button(action: {
                handleCommandSubmission()
            }) {
                Text("Submit")
                ...
            }
...
// Function to handle the command submission
private func handleCommandSubmission() {
    isTextFieldDisabled = true  // Disable the text field
    handleCommand(commandText)  // Handle the command
    
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
        isTextFieldDisabled = false  // Re-enable the text field after 100ms
    }
}

Upvotes: 0

Sagar D
Sagar D

Reputation: 223

If someone is still facing the issue, the cursor remains if resignFirstResponder() is not called from main queue.

So you just need to:

DispatchQueue.main.async {
  textView.resignFirstResponder()
}

Upvotes: 1

Mike Gledhill
Mike Gledhill

Reputation: 29161

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.

Upvotes: 3

Fonix
Fonix

Reputation: 11597

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

Upvotes: 1

IOS DEV
IOS DEV

Reputation: 126

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

Upvotes: 5

Related Questions