Close iOS Keyboard by touching anywhere using Swift -
i have been looking on can't seem find it. know how dismiss keyboard using objective-c
have no idea how using swift
? know?
override func viewdidload() { super.viewdidload() //looks single or multiple taps. let tap: uitapgesturerecognizer = uitapgesturerecognizer(target: self, action: "dismisskeyboard") //uncomment line below if want tap not not interfere , cancel other interactions. //tap.cancelstouchesinview = false view.addgesturerecognizer(tap) } //calls function when tap recognized. func dismisskeyboard() { //causes view (or 1 of embedded text fields) resign first responder status. view.endediting(true) }
========================================================================
edit: here way task if going use functionality in multiple uiviewcontrollers:
// put piece of code anywhere extension uiviewcontroller { func hidekeyboardwhentappedaround() { let tap: uitapgesturerecognizer = uitapgesturerecognizer(target: self, action: #selector(uiviewcontroller.dismisskeyboard)) tap.cancelstouchesinview = false view.addgesturerecognizer(tap) } func dismisskeyboard() { view.endediting(true) } }
now in every uiviewcontroller, have call function:
override func viewdidload() { super.viewdidload() self.hidekeyboardwhentappedaround() }
this function included standard function in repo contains lot of useful swift extensions one, check out: https://github.com/goktugyil/ezswiftextensions
Comments
Post a Comment