Saturday, 15 August 2015

ios - Scrollview and keyboard Swift -



ios - Scrollview and keyboard Swift -

i'm new here , im starting swift ios.

i started creating simple app operations. i'm having problems when keyboard appears, hiding 1 of textfields. think it's mutual problem , did research couldn't find solved problem. , want utilize scroll rather animate textfield create visible.

thanks!!!!! (sorry english language mistakes)

in viewdidload, register notifications:

nsnotificationcenter.defaultcenter().addobserver(self, selector: "keyboardwillshow:", name:uikeyboardwillshownotification, object: nil) nsnotificationcenter.defaultcenter().addobserver(self, selector: "keyboardwillhide:", name:uikeyboardwillhidenotification, object: nil)

add below observer methods automatic scrolling when keyboard appears.

func textfieldshouldreturn(textfield: uitextfield) -> bool { textfield.resignfirstresponder() homecoming true } func keyboardwillshow(notification:nsnotification){ var userinfo = notification.userinfo! var keyboardframe:cgrect = (userinfo[uikeyboardframebeginuserinfokey] as! nsvalue).cgrectvalue() keyboardframe = self.view.convertrect(keyboardframe, fromview: nil) var contentinset:uiedgeinsets = self.scrollview.contentinset contentinset.bottom = keyboardframe.size.height self.scrollview.contentinset = contentinset } func keyboardwillhide(notification:nsnotification){ var contentinset:uiedgeinsets = uiedgeinsetszero self.scrollview.contentinset = contentinset }

ios view swift scroll

No comments:

Post a Comment