How to make return key on iPhone make keyboard disappear?

后端 未结 14 539
有刺的猬
有刺的猬 2020-12-02 07:06

I have two UITextFields (e.g. username and password) but I cannot get rid of the keyboard when pressing the return key on the keyboard. How can I do this?

14条回答
  •  心在旅途
    2020-12-02 07:53

    Swift 2 :

    this is what is did to do every thing !

    close keyboard with Done button or Touch outSide ,Next for go to next input.

    First Change TextFiled Return Key To Next in StoryBoard.

    override func viewDidLoad() {
      txtBillIdentifier.delegate = self
      txtBillIdentifier.tag = 1
      txtPayIdentifier.delegate  = self
      txtPayIdentifier.tag  = 2
    
      let tap = UITapGestureRecognizer(target: self, action: "onTouchGesture")
      self.view.addGestureRecognizer(tap)
    
    }
    
    func textFieldShouldReturn(textField: UITextField) -> Bool {
       if(textField.returnKeyType == UIReturnKeyType.Default) {
           if let next = textField.superview?.viewWithTag(textField.tag+1) as? UITextField {
               next.becomeFirstResponder()
               return false
           }
       }
       textField.resignFirstResponder()
       return false
    }
    
    func onTouchGesture(){
        self.view.endEditing(true)
    }
    

提交回复
热议问题