Reputation: 145
I am so sorry to post this, since it has been asked a million of times, but whenever i try to use one of the solutions, it is either outdated, or won't work with my code. (or maybe i am just doing it wrong)
i have 5 textfields and a textview, that go into an email, and i am trying to minimise the keyboard when im done, but i dont know how to.
I also tried to make it go to the next text field, until the last one, but that doesn't need to be done.
func textView(textView: UITextView, shouldChangeTextInRange: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
text6.resignFirstResponder()
return false
}
return true
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == self.text1 {
self.text2.becomeFirstResponder()
}
else if textField == self.text2 {
self.text3.becomeFirstResponder()
}
else if textField == self.text3 {
self.text4.becomeFirstResponder()
}
else if textField == self.text4 {
self.text5.becomeFirstResponder()
self.text5.resignFirstResponder();
}
return true;
}
Upvotes: 10
Views: 11823
Reputation: 189
First you have to Confirm the UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool
{
textField.resignFirstResponder()
return true
}
Upvotes: 1
Reputation:
first you have to add UITextFieldDelegate
in .h
then in .m
yourtextfield.delegate = self
then apply this code
func textFieldShouldReturn(textField: UITextField!) -> Bool {
textField.resignFirstResponder()
return true;
}
Upvotes: 14
Reputation: 1818
Sometimes will be helpful use both ways (Swift 4):
First use declare a delegate UITextFieldDelegate
where you have textFieldShouldReturn
method.
Second you have to set self.textField.delegate = self
in initialization
part (as usual in viewWillAppear(_ animated: Bool)
or viewDidLoad
).
And then fill method as:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.resignFirstResponder()
self.view.endEditing(true)
//or use on superView of textField
//superView.endEditing(true)
return true
}
Upvotes: 1
Reputation: 532
@Peter Boesen:
To minimise the keyboard when done. i.e. when "return" key is pressed... you would need x2 things before making comments in the main part of the code...
At Top of the View Controller (code) where you have class ViewController: UIViewController
- add UITextFieldDelegate
. So at the end it should look like:
class ViewController: UIViewController, UITextFieldDelegate
in the same View Controller (code) in the func viewDidLoad
add
self.textField.delegate = self
Then... as mentioned by 'Abhinandan Pratap', incorporate the below code to control behaviour of the KeyBoard when pressing 'Return':
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
However, if you want to minimize the Keyboard by Tapping elsewhere on the screen, the following on its own will suffice by using 'TouchesBegan' (provided you're not using scroll view)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
If your using scrollView for the screen, you need to use self.view.endEditing(true)
in conjunction with UITapGesture
to making tapping outside close down the keyboard.
Upvotes: 8