ios - How to make UITextField move up when keyboard is present? -


how prevent uitextfield being hidden keyboard?

i assume happening on uiviewcontroller. if so, can setup following 2 functions called when keyboard show/hide, , respond appropriately in blocks.

setting uiviewcontroller:

class xxxviewcontroller: uiviewcontroller, uitextfielddelegate... {      var frameview: uiview! 

first, in viewdidload:

override func viewdidload() {      self.frameview = uiview(frame: cgrectmake(0, 0, self.view.bounds.width, self.view.bounds.height))       // keyboard stuff.     let center: nsnotificationcenter = nsnotificationcenter.defaultcenter()         center.addobserver(self, selector: #selector(atreportcontentviewcontroller.keyboardwillshow(_:)), name: uikeyboardwillshownotification, object: nil)     center.addobserver(self, selector: #selector(atreportcontentviewcontroller.keyboardwillhide(_:)), name: uikeyboardwillhidenotification, object: nil) } 

then implement following 2 functions respond nsnotificationcenter functions defined in viewdidload above. give example of moving entire view, can animate uitextfields.

func keyboardwillshow(notification: nsnotification) {     let info:nsdictionary = notification.userinfo!     let keyboardsize = (info[uikeyboardframebeginuserinfokey] as! nsvalue).cgrectvalue()      let keyboardheight: cgfloat = keyboardsize.height      let _: cgfloat = info[uikeyboardanimationdurationuserinfokey] as! nsnumber cgfloat       uiview.animatewithduration(0.25, delay: 0.25, options: uiviewanimationoptions.curveeaseinout, animations: {         self.frameview.frame = cgrectmake(0, (self.frameview.frame.origin.y - keyboardheight), self.view.bounds.width, self.view.bounds.height)         }, completion: nil)  }  func keyboardwillhide(notification: nsnotification) {     let info: nsdictionary = notification.userinfo!     let keyboardsize = (info[uikeyboardframebeginuserinfokey] as! nsvalue).cgrectvalue()      let keyboardheight: cgfloat = keyboardsize.height      let _: cgfloat = info[uikeyboardanimationdurationuserinfokey] as! nsnumber cgfloat      uiview.animatewithduration(0.25, delay: 0.25, options: uiviewanimationoptions.curveeaseinout, animations: {         self.frameview.frame = cgrectmake(0, (self.frameview.frame.origin.y + keyboardheight), self.view.bounds.width, self.view.bounds.height)         }, completion: nil)  } 

don't forget remove notifications when leaving view

override func viewwilldisappear(animated: bool) {     nsnotificationcenter.defaultcenter().removeobserver(self, name: uikeyboardwillshownotification, object: nil)     nsnotificationcenter.defaultcenter().removeobserver(self, name: uikeyboardwillhidenotification, object: nil) } 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -