0

I am using TextFieldEffects for custom UITextFields. Creating the textfield was pretty straight forward..

class ViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
        let textField = HoshiTextField(frame: textFieldFrame)
        textField.placeholderColor = .darkGrayColor()
        textField.foregroundColor = .lightGrayColor()

        view.addSubView(textField) 
   }
}

For dismissing the keyboard, I tried to do what I usually do, but unfortunately no luck.

func textFieldShouldReturn(textField: HoshiTextField) -> Bool {
    self.view.endEditing(true)
    return false
}

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

Do you have any suggestions?

senty
  • 12,385
  • 28
  • 130
  • 260

3 Answers3

2

Confirm the UITextFieldDelegate protocol to your ViewController and assign textField.delegate = self and then implement the following delegate method:

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

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

For better understanding please see my code snipetenter image description here

Muzahid
  • 5,072
  • 2
  • 24
  • 42
  • By adding the delegate = self, `textFieldShouldReturn()` started working. However, `dismissKeyboard()` (clicking outside) is still not working. – senty Mar 15 '16 at 02:02
  • implement touch began method override func touchesBegan(touches: Set, withEvent event: UIEvent?) { self.view.endEditing(true) } – Muzahid Mar 15 '16 at 02:05
  • Can you please include it in your answer so I can accept it as the right answer? – senty Mar 15 '16 at 02:06
  • I used (this answer)[http://stackoverflow.com/a/32281860/4705339] instead as your one gave me an error. But thanks anyways – senty Mar 15 '16 at 02:12
1
class ViewController: UIViewController, UITextFieldDelegate //add this protocol 
{
...
  func ... {
    let textField = HoshiTextField(frame: textFieldFrame)
    textField.placeholderColor = .darkGrayColor()
    textField.foregroundColor = .lightGrayColor()
    textField.delegate = self
  }
}
tuledev
  • 10,177
  • 4
  • 29
  • 49
  • I tried this too, but I receive error "Cannot assign value of type 'YourViewController' to type 'UITextFieldDelegate?'" What may be the workaround? – senty Mar 15 '16 at 01:45
  • `textField.delegate = self` this is where the error occurs. I think the answer is defining it at the top (like if I created the UITextField on Storyboard, I would ctrl+drag, but as I created it programatically, maybe I need to define it at the top as well). What do you think? – senty Mar 15 '16 at 01:48
  • @senty You don't need to do that. You can create like what you are doing. Please post code. Maybe full class `ViewController` – tuledev Mar 15 '16 at 01:50
  • **Update:** The return button (`textFieldShouldReturn`) works now, however not the other one (`dismissKeyboard`, when I click outside) – senty Mar 15 '16 at 01:51
  • @senty For click outside, you have to search more. You have to do somethings more. It is another problem. Please mark what is the right answer and ask another question. thanks you. You can refer dismisskeyboard here: http://stackoverflow.com/questions/32281651/how-to-dismiss-keyboard-when-touching-anywhere-outside-uitextfield-in-swift – tuledev Mar 15 '16 at 01:58
0

Set delegate for your textField and maybe it will work!

Nhat Dinh
  • 3,378
  • 4
  • 35
  • 51
  • How can I do it? It's embedded in UIViewController. I tried using textField.delegate = self but it doesn't let me do it because self is UIViewController. What is the proper way of doing it? Thanks – senty Mar 15 '16 at 01:38
  • set something like : `class YourViewController: UIViewController, UITextFieldDelegate` refer: http://sourcefreeze.com/uitextfield-and-uitextfield-delegate-in-swift/ – Nhat Dinh Mar 15 '16 at 01:43