Hiding the Keyboard when losing focus on a UITextView

后端 未结 10 535
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 10:34

So I have a UITextView that I\'m using to allow the user to submit some text.

My problem is, I can\'t seem to figure out how to allow the user to \'Cancel\' by tappi

10条回答
  •  离开以前
    2020-12-04 10:56

    Here is the swift 3 code

    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        if let touch = touches.first {
            if textField.isFirstResponder && touch.view != textField {
                textField.resignFirstResponder()
            }
        }
        super.touchesBegan(touches, with: event)
    }
    

提交回复
热议问题