ios - can't get any of the responses for scrolling textviews when keyboard presents from stack overflow to work -
i don't know why attempts scrollview move textviews remain visible when keyboard presents via stack overflow responses haven't been working, haven't me. translating apple documentation below swift should enough me work, don't know objective c enough this. willing translate following swift 3?
1.// call method somewhere in view controller setup code. - (void)registerforkeyboardnotifications { [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwasshown:) name:uikeyboarddidshownotification object:nil]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwillbehidden:) name:uikeyboardwillhidenotification object:nil]; } 2. // called when uikeyboarddidshownotification sent. - (void)keyboardwasshown:(nsnotification*)anotification { nsdictionary* info = [anotification userinfo]; cgsize kbsize = [[info objectforkey:uikeyboardframebeginuserinfokey] cgrectvalue].size; uiedgeinsets contentinsets = uiedgeinsetsmake(0.0, 0.0, kbsize.height, 0.0); scrollview.contentinset = contentinsets; scrollview.scrollindicatorinsets = contentinsets; // if active text field hidden keyboard, scroll it's visible // app might not need or want behavior. cgrect arect = self.view.frame; arect.size.height -= kbsize.height; if (!cgrectcontainspoint(arect, activefield.frame.origin) ) { [self.scrollview scrollrecttovisible:activefield.frame animated:yes]; } } 3.// called when uikeyboardwillhidenotification sent - (void)keyboardwillbehidden:(nsnotification*)anotification { uiedgeinsets contentinsets = uiedgeinsetszero; scrollview.contentinset = contentinsets; scrollview.scrollindicatorinsets = contentinsets; }
here go. have used code uiview, though. should able make adjustments scrollview.
func addkeyboardnotifications() { notificationcenter.default.addobserver(self, selector: #selector(keyboardwillshow(notification:)), name: nsnotification.name.uikeyboardwillshow, object: nil) notificationcenter.default.addobserver(self, selector: #selector(keyboardwillhide(notification:)), name: nsnotification.name.uikeyboardwillhide, object: nil) } func keyboardwillshow(notification: nsnotification) { if let keyboardsize = (notification.userinfo?[uikeyboardframeenduserinfokey] as? nsvalue)?.cgrectvalue { let duration = notification.userinfo![uikeyboardanimationdurationuserinfokey] as! double // if using constraints // bottomviewbottomspaceconstraint.constant = keyboardsize.height self.view.frame.origin.y -= keyboardsize.height uiview.animate(withduration: duration) { self.view.layoutifneeded() } } } func keyboardwillhide(notification: nsnotification) { let duration = notification.userinfo![uikeyboardanimationdurationuserinfokey] as! double //if using constraint // bottomviewbottomspaceconstraint.constant = 0 self.view.frame.origin.y += keyboardsize.height uiview.animate(withduration: duration) { self.view.layoutifneeded() } } don't forget remove notifications @ right place.
func removekeyboardnotifications() { notificationcenter.default.removeobserver(self, name: nsnotification.name.uikeyboardwillshow, object: nil) notificationcenter.default.removeobserver(self, name: nsnotification.name.uikeyboardwillhide, object: nil) }
Comments
Post a Comment