ios - tableView vs. scrollView behavior when keyboard appears -
if textfield opens keyboard , textfield member of tableview, able scroll tableview able see last item, , keyboard not hide item.
how? uitableview inherited uiscrollview. guess opening keyboard increases content offset? right?
if textfield part of scrollview, not tableview, effect not occur, , keyboard can hide out other controls positioned lower parts of scrollview.
if want see same effect scrollview tableview, should set content offset manually?
yes have set content offset manually .
first register notification
nsnotificationcenter.defaultcenter().addobserver(self, selector: "keyboardwillshow:", name:uikeyboardwillshownotification, object: nil) nsnotificationcenter.defaultcenter().addobserver(self, selector: "keyboardwillhide:", name:uikeyboardwillhidenotification, object: nil)
and in observer methods.
func textfieldshouldreturn(textfield: uitextfield) -> bool { textfield.resignfirstresponder() return 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 }
Comments
Post a Comment