ios - UITextField keyboard not dismissing -
i have uiviewcontroller
uitextfield
in , i'm trying dismiss keyboard when click away or view dismissed. however, when call resignfirstresponder()
, keyboard still doesn't dismiss , i'm not quite sure why. here's code:
class messageviewcontroller: uiviewcontroller, uitextfielddelegate { var messagetextfield : uitextfield = uitextfield() override func viewdidload() { ... messagetextfield.frame = cgrectmake(10, uiscreen.mainscreen().bounds.size.height-50, uiscreen.mainscreen().bounds.size.width-80, 40) messagetextfield.delegate = self messagetextfield.borderstyle = uitextborderstyle.line messagetextfield.becomefirstresponder() self.view.addsubview(messagetextfield) nsnotificationcenter.defaultcenter().addobserver(self, selector: "keyboardwillshow:", name: uikeyboardwillshownotification, object: nil) nsnotificationcenter.defaultcenter().addobserver(self, selector: "keyboardwillhide:", name: uikeyboardwillhidenotification, object: nil) ... } func textfieldshouldreturn(textfield: uitextfield) -> bool { textfield.resignfirstresponder() return true } override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { self.view.endediting(true) println("touched") } func keyboardwillshow(notification: nsnotification) { var keyboardsize = (notification.userinfo?[uikeyboardframebeginuserinfokey]as? nsvalue)?.cgrectvalue() let messageframe = messagetextfield.frame let newy = messageframe.origin.y - keyboardsize!.height messagetextfield.frame = cgrectmake(messageframe.origin.x, newy, messageframe.size.width, messageframe.size.height) } func keyboardwillhide(notification: nsnotification) { var keyboardsize = (notification.userinfo?[uikeyboardframeenduserinfokey]as? nsvalue)?.cgrectvalue() let messageframe = messagetextfield.frame let newy = messageframe.origin.y - keyboardsize!.height messagetextfield.frame = cgrectmake(messageframe.origin.x, newy, messageframe.size.width, messageframe.size.height) } }
does know why keyboard isn't dismissing? added uitextfield view programmatically opposed using storyboard. make difference?
class viewcontroller: uiviewcontroller,uitextfielddelegate {
confirm protocols
messagetextfield.delegate=self
set delegate
func textfieldshouldreturn(textfield: uitextfield!) -> bool { messagetextfield.resignfirstresponder() return true; }
use code..
Comments
Post a Comment