How to Dismiss the Keyboard When Editing a Uitextfield

How do you dismiss the keyboard when editing a UITextField

I set the delegate of the UITextField to my ViewController class.

In that class I implemented this method as following:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}

How to dismiss keyboard with multiple UITextField

Here's one option... We're going to add a boolean flag to determine whether or not we're in a textField when an edit attempt for another textField begins

Make your class adhere to UITextFieldDelegate

class MyClass: UIViewController, UITextFieldDelegate

Don't forget to set the delegate, we'll add the flag as well

myTextField.delegate = self
var inField = false

Implement "textFieldShouldBeginEditing" and "textFieldDidBeginEditing"

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if inField {
inField = false
return false
}
return true
}

func textFieldDidBeginEditing(textField: UITextField) {
inField = true
}

I prefer tracking things like this rather than identifying subviews as it allows the flag to be utilized elsewhere and cuts down code complexity.

Swift: Can't get UITextField to dismiss keyboard

You need to call resignFirstResponder inside textFieldShouldReturn method instead of calling textFieldDidEndEditing.

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}

Also in your TapGesture method simply call endEditing(_:) with your view instead of looping through the array of textFields.

func dismiss(_ sender:UITapGestureRecognizer) {
self.view.endEditing(true)
}

Dismiss Keyboard when editing UItextfield on Swift

Edit

This workaround was specific for the above question , so stop downvote this answer. This is not about simply dismissing the keyboard. This is about being able to edit the textfield with custom inputView and make the textfield editable when there is no keyboard visible.

This makes your textfields keyboard disappear and still editable .

Objective-C

yourTextField.inputView = [[UIView alloc] initWithFrame:CGRectZero];

Swift

yourTextField.inputView = UIView(frame: CGRectZero)

Hide Keyboard on Textfield when it Editing

1.Set the textField delegate like below image

(A)

Sample Image

(B)

Sample Image

2.Add textField delegate methods

Swift

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
// Implement your Date Time Picker initial Code here
return false
}

Objective C

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
// Implement your Date Time Picker initial Code here
return NO;
}

SwiftUI Dismiss Keyboard from UITextField

So I found a trick on my own with an epiphany overnight.

First, I would like to share to anyone else a very basic reason why inb4cookies solution wasn't quite adequate. While I had already tried adding a resignFirstResponder call like it to the onTap of the background stack, it was triggering the onTap for the VStack when I was clicking the field.

This is likely because I am using a UITextField as the back end for this component and not a SwiftUI TextField.

However, it was partially used in the final solution. I still applied it, but there is an extra step.

VStack {
CustomTextView($viewModel.parameter)
.onTap {/*Insert literally any compiling code here*/ }
/// Other views
}
.onTap {
self.hideKeyboard()
}

You'll see that above, there is an extra onTap. I tested it with a print statement, but this will override the onTap for the VStack and prevent the keyboard from being dismissed right after it is brought up. Tapping anywhere else on the VStack still closes it, except for Buttons. But I can always add hideKeyboard to those buttons if needed.

How to dismiss keyboard when touching anywhere outside UITextField (in swift)?

Edited for Swift 4

Edit: Added @objc. While this isn't the best option for performance, one instance of it here shouldn't cause too many problems until there is a better solution.

Edited to fix when needing to interact with items behind GestureRecognizer.

Edit: Thanks @Rao for pointing this out. Added tap.cancelsTouchesInView = false.

This should help you with having multiple UITextView or UITextField

Create an extension of the view controller. This has worked much smoother for me and with less hassle than trying to use .resignFirstResponder()

extension UIViewController
{
func setupToHideKeyboardOnTapOnView()
{
let tap: UITapGestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(UIViewController.dismissKeyboard))

tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}

@objc func dismissKeyboard()
{
view.endEditing(true)
}
}

Call self.setupToHideKeyboardOnTapOnView() in the viewDidLoad

UITextView - Keyboard Dismiss on Drag or Dismiss Interactively

For the built-in keyboard dismissing to work for a UIScollView or one of its subclasses (e.g. UITextView) the scroll view needs to be able to scroll. If there's not enough text in the text view to provide for scrolling, it won't perform a keyboard dismiss.

However, you can turn on vertical bouncing and then it will work. Check "Bounce Vertically" in interface builder or in code set myTextView.alwaysBounceVertical = true.



Related Topics



Leave a reply



Submit