Hiding the Keyboard when losing focus on a UITextView

后端 未结 10 550
爱一瞬间的悲伤
爱一瞬间的悲伤 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:47

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        var touch = event.allTouches()?.anyObject() as UITouch
    
        if( textfield.isFirstResponder() && touch.view != textfield) {
            textfield.resignFirstResponder()
            NSLog("Resigning first responder since touches began outside")
        }
    
        super.touchesBegan(touches, withEvent: event)
    }
    

    ohhorob's answer worked for me too. Here's the simple swift equivalent.

提交回复
热议问题