hide keyboard for text field in swift programming language

后端 未结 14 980
不思量自难忘°
不思量自难忘° 2020-11-27 17:07

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 te         


        
相关标签:
14条回答
  • 2020-11-27 17:42

    Just override the UIViewController method called "touchesBegan" and set endEditing to true. Just like this:

    override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
        self.view.endEditing(true)
    }
    
    0 讨论(0)
  • 2020-11-27 17:42

    First you need to set delegate for your textfield then you need to include resignFirstResponder() to hide keyboard when press return button of keybaord.

     func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
     {
       textField .resignFirstResponder()
       return true;
     }
    
    0 讨论(0)
  • 2020-11-27 17:42

    You can try this code to have the "return" key execute code.

    func textFieldShouldReturn(textField: UITextField) -> Bool{
        textField.resignFirstResponder()
        performAction()
        return true;
    }
    
    func performAction(){
    
    //execute code for your action inside this function
    
    }
    

    Hope this can help you.

    0 讨论(0)
  • 2020-11-27 17:47
    UIApplication.sharedApplication().sendAction(#selector(UIResponder.resignFirstResponder), to:nil, from:nil, forEvent:nil)
    
    0 讨论(0)
  • 2020-11-27 17:48

    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.

    0 讨论(0)
  • 2020-11-27 17:50

    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.

    0 讨论(0)
提交回复
热议问题