Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 698 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hide keyboard for text field in swift programming language

#1
I have little experience in Objective-C. I want to hide the keyboard for a text field using the Swift programming language.

I also tried this

func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
{
return true;
}

But the method is not getting triggered when I hit return. Anyone have any luck with this?
Reply

#2
when you call your UIAlertController, place condition in the completion handler if you'd like to hide the keyboard.

self.present('nameOfYourUIAlertController', animated: true, completion: {
if condition == true {
'nameOfYourUIAlertController'.textFields![0].resignFirstResponder()
}
})

This works great for me.
Reply

#3
Now In Swift 3/4/5, the easiest methods are

**Method 1:** called when 'return' key pressed.

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

**Method 2:** called when 'return' key pressed.

func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
self.view.endEditing(true)
return true;
}

**Method 3:** Global Method Write in view did load

self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))

**Note:** Don't forget to add this. Other wise its not work.

UIGestureRecognizerDelegate, UITextFieldDelegate

I hope it will work for you :)
Reply

#4
In simple way to hide the keyboard just override the UIView "touchBegan method". So when you tap any where in the view keyboard is hide.
here is the sample code.. (Swift 3.0 Updated code)

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
self.view.endEditing(true)
}
Reply

#5
I think the Problem is with setting the Delegate.

Please set textfield Delegate to your ViewController like this

class ViewController: UIViewController,UITextFieldDelegate {

and then create the IBOutlet for your text field like this

@IBOutlet var txtTest : UITextField = nil

make sure that it is connected to your text field on your view in storyboard.

finally set its Delegate using

txtTest.delegate=self

We are almost done. Now place this delegate function into your class.



func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
{
textField.resignFirstResponder()
return true;
}

Hope this will solve your issue.



Reply

#6
In order to hide the keyboard when you pressed a button, you need to use the following function.

self.view.endEditing(true)

This works when a button is pressed.

Hope this helps someone out there.
Reply

#7
UIApplication.sharedApplication().sendAction(#selector(UIResponder.resignFirstResponder), to:nil, from:nil, forEvent:nil)
Reply

#8
Here i have solved the problem, that when **keypad opens and the view gets hidden behind the keypad**.... **that time we need to change the UI constraints dynamically** in order to make the whole view visible. In my case i am changing the bottonConstraint of the `UITextField` dynamically.

*So, here goes the complete code. I have one `UITextView` and `UITextField` on `UIViewController` to work on for just testing...*


import UIKit

class ViewController: UIViewController, UITextViewDelegate, UITextFieldDelegate {

@IBOutlet weak var textView: UITextField!

@IBOutlet weak var textField: UITextView!

/*Height Constraint for textField*/
@IBOutlet weak var bottom: NSLayoutConstraint!

var defaultHeight:CGFloat = 0.0;

override func viewDidLoad() {
super.viewDidLoad()

defaultHeight = bottom.constant;

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);


NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);

}

override func resignFirstResponder() -> Bool {
textView.resignFirstResponder();
textField.resignFirstResponder();
return true;
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
resignFirstResponder()
print("touched began")
}

func textViewDidEndEditing(textView: UITextView) {
resignFirstResponder()
print("text view did end editing")
}

func textViewShouldEndEditing(textView: UITextView) -> Bool {
resignFirstResponder()
print("text view should end editing")
return true;
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
print("text field should return")
resignFirstResponder()
return true;
}

func textFieldDidEndEditing(textField: UITextField) {
resignFirstResponder()
print("did end editing")
}

func keyboardWillShow(notification: NSNotification) {
print("keyboard will show.............")
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
let a = keyboardSize.height;
self.bottom.constant = a + defaultHeight;
self.view.updateConstraints();
}
}

func keyboardWillHide(nofication:NSNotification)
{
print("keyboard will hide................")
bottom.constant = defaultHeight;
self.view.updateConstraints();

}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Reply

#9
You can also do like this:
add event "Did end on exit" from connections inspector of your TextField
and connect it to method from relevant ViewController

*In my example I connect it to method called 'ended'*

declare sender as UITextField and then call sender.resignFirstResponder();
Like this:

@IBAction func ended (sender: UITextField){ sender.resignFirstResponder(); }


Reply

#10
- Add UITextFieldDelegate to the class declaration:

class ViewController: UIViewController, UITextFieldDelegate

- Connect the textfield or write it programmatically

@IBOutlet weak var userText: UITextField!

- set your view controller as the text fields delegate in view did load:

override func viewDidLoad() {
super.viewDidLoad()
self.userText.delegate = self
}

- Add the following function

func textFieldShouldReturn(userText: UITextField!) -> Bool {
userText.resignFirstResponder()
return true;
}
with all this your keyboard will begin to dismiss by touching outside the textfield aswell as by pressing return key.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through