0

My code inside a UIView Class that contains a UITextField named titleInput:

override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
    // Code to Dismiss the Keyboard when Pressed Outside Text Field
    if !titleInput.pointInside(point, withEvent: event) {
        endEditing(true)
    }
    
    // Return Original hitTest Result as Usual
    return super.hitTest(point, withEvent: event)
}

I am experiencing a strange bug. When I first start the app, everything works as expected, touching outside the titleInput dismisses the keyboard. However, it brakes if I switch to a different app, and than come back to this app. After coming back to the app, clicking on the keyboard also dismisses the keyboard. Makes it difficult to type :)

Any idea why this is happening, and why it ONLY starts to happen after switching away from the app and then coming back to it? Also, is there a better way to do this same thing.

Flimzy
  • 68,325
  • 15
  • 126
  • 165
Bogdan
  • 559
  • 4
  • 20

2 Answers2

0

I would recommended doing it this way instead of what you are trying. This will just dismiss the keyboard when the enter/return key is pressed. I realize it is in Obj-C however the method calls are almost identical.

Community
  • 1
  • 1
Patrick Zawadzki
  • 426
  • 2
  • 6
  • 25
  • Yep, I have done that. Actually I am using the return key to add new item. So if return is pressed, the save function is fired. I wanted to add tap anywhere else to cancel the action instead. – Bogdan Jun 11 '15 at 00:43
0

I usually do just this

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    view.endEditing(true)
    super.touchesBegan(touches, withEvent: event)
}
membersheep
  • 504
  • 4
  • 12