ios - How to test if keyboard is covering UITextField in UITableView? -
how can check if keyboard covering first responder inside uiscrollview
may or may not uitableview
? note uiscrollview
not cover entire viewcontroller's view , may contained in modal view (uimodalpresentationformsheet
).
i'm using modified code apple's reference documentation , example, cgrectcontainspoint
return false when keyboard covering first responder. it's obvious i'm not using convertrect:toview
correctly.
also, apple's code not take account view not full-screen, setting scrollview's contentinset full height of keyboard isn't great solution -- should inset portion of keyboard covering firstresponder.
- (void)keyboardwasshown:(nsnotification*)anotification { // self.scrollview can tableview or not. need handle both uiview *firstresponderview = [self.scrollview findfirstresponder]; if (!firstresponderview) return; nsdictionary* info = [anotification userinfo]; cgrect rect = [[info objectforkey:uikeyboardframeenduserinfokey] cgrectvalue]; // convertrect:toview? convertrect:fromview? confusing cgrect kbrect = [self.scrollview convertrect:rect toview:nil]; cgrect viewrect = [self.scrollview convertrect:firstresponderview.bounds toview:nil]; // doesn't work. convertrect misuse blame if (!cgrectcontainspoint(kbrect, firstresponderview.frame.origin)) return; // inset portion keyboard covers? uiedgeinsets contentinsets = uiedgeinsetsmake(0.0, 0.0, kbrect.size.height, 0.0); self.scrollview.contentinset = contentinsets; self.scrollview.scrollindicatorinsets = contentinsets; }
without further testing or having deep @ logic, line seems odd:
cgrect kbrect = [self.scrollview convertrect:rect toview:nil];
the keyboard rect (that included in notification) in window coordinates , want convert scroll view coordinate system. [viewa convertrect:rect toview:viewb]
converts rect viewa's coordinate system viewb's coordinate system, doing opposite of should doing (as suspected).
what i'm doing this:
- (void)keyboardwillshow:(nsnotification *)anotification { nsdictionary *info = [anotification userinfo]; cgrect kbrect = [[info objectforkey:uikeyboardframeenduserinfokey] cgrectvalue]; kbrect = [self.view.window convertrect:kbrect toview:self.view]; // convert local coordinate system, otherwise in window coordinates , not consider interface orientation _keyboardsize = kbrect.size; // store later use [uiview animatewithduration:0.25 animations:^{ uiedgeinsets insets = uiedgeinsetsmake(0.0f, 0.0f, max(0.0f, cgrectgetmaxy(_tableview.frame) - cgrectgetminy(kbrect)), 0.0f); // nb: _tableview direct subview of self.view, _tableview.frame , kbrect in same coordinate system _tableview.contentinset = insets; _tableview.scrollindicatorinsets = insets; [self scrolltoactivetextfield]; // here adapt content offset ensure active text field visible }]; }
Comments
Post a Comment