user1998511
user1998511

Reputation: 465

UITextField keyboard not dismissing

I have a UIViewController with a UITextField in it and I'm trying to dismiss the keyboard when I click away or the view is dismissed. However, when I call resignFirstResponder(), the keyboard still doesn't dismiss and I'm not quite sure why. Here's my code:

class MessageViewController: UIViewController, UITextFieldDelegate {
    var messageTextField : UITextField = UITextField()

    override func viewDidLoad() {
        ...
        messageTextField.frame = CGRectMake(10, UIScreen.mainScreen().bounds.size.height-50, UIScreen.mainScreen().bounds.size.width-80, 40)
    messageTextField.delegate = self
    messageTextField.borderStyle = UITextBorderStyle.Line
    messageTextField.becomeFirstResponder()
    self.view.addSubview(messageTextField)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
        ...
    }

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

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        self.view.endEditing(true)
        println("Touched")
    }

    func keyboardWillShow(notification: NSNotification) {
        var keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey]as? NSValue)?.CGRectValue()
        let messageFrame = messageTextField.frame
        let newY = messageFrame.origin.y - keyboardSize!.height
        messageTextField.frame = CGRectMake(messageFrame.origin.x, newY, messageFrame.size.width, messageFrame.size.height)
    }

    func keyboardWillHide(notification: NSNotification) {
        var keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey]as? NSValue)?.CGRectValue()
        let messageFrame = messageTextField.frame
        let newY = messageFrame.origin.y - keyboardSize!.height
        messageTextField.frame = CGRectMake(messageFrame.origin.x, newY, messageFrame.size.width, messageFrame.size.height)
    }

}

Does anyone know why the keyboard isn't dismissing? I added the UITextField to the view programmatically as opposed to using storyboard. Does that make a difference?

Upvotes: 1

Views: 4911

Answers (5)

very simple. first you add Tap Gesture

var tap = UITapGestureRecognizer(target: self, action: "handle:")
view.addGestureRecognizer(tap)

in function handle

func handle(tap: UITapGestureRecognizer){
view.endEditing(true)
}

so you can dismiss keyboard when you click outside UITextField

Upvotes: 0

user3353890
user3353890

Reputation: 1891

@vijeesh's answer will probably work, and the logic is almost correct, but it is technically wrong if you ever use more than one UITextField. textField is the UITextField parameter that is passed when textFieldShouldReturn is called. The problem is, you're just declaring:

textField.resignFirstResponder()

Your program doesn't know what UITextField you're referring to. Even though you may only have one textField in your program, and you know that it's your messageTextField, the program still doesn't know that. It just sees "textField". So you have have to tell it what to do for each UITextField in your program. Even if you have just one. This should work:

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

Upvotes: 0

Yuyutsu
Yuyutsu

Reputation: 2527

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

check delegate add into your viewDidLoad()

self.yourTextField.delegate = self;

how-to-dismiss-uitextfields-keyboard-in-your-swift-app

This might helps you :)

Upvotes: 0

Yu Yu
Yu Yu

Reputation: 1369

The following is how I did for that problem. I hope this method solve your problem.

textfield

@IBOutlet var userID : UITextField!

function.

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

In your desired function, you need to write this syntax.

@IBAction func login(sender: AnyObject)
{
    userID.resignFirstResponder( )
}

This is in ViewDidLoad or ViewDidAppear

override func viewDidLoad( )
{
    userID.delegate = self;
}

I cannot comment on the previous user. That's why I write this one. I hope this gives you idea.

Upvotes: 1

vijeesh
vijeesh

Reputation: 1327

 class ViewController: UIViewController,UITextFieldDelegate {

confirm protocols

messageTextField.delegate=self

set delegate

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

Use this code..

Upvotes: 4

Related Questions