Why does UIScrollView resize back up when moving to another textfield - iOS iPhone -
this isn't easy 1 answer....
i have 8 text fields on screen sit in scrollview. when select first field, keyboard appears , scrollview shrinks. can scroll , down.
when select text field, can't scroll anymore.
i've checked scrollview height using textfielddidbeginediting , grows up.
i've registered 4 of keyboard notifications none of them triggered during move new text field.
any pointers appreciated :)
- (void)viewdidload { [super viewdidload]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwillshow:) name:uikeyboardwillshownotification object:self.view.window]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwillhide:) name:uikeyboardwillhidenotification object:self.view.window]; keyboardisshown = no; //additional code } - (void)viewdidunload { [[nsnotificationcenter defaultcenter] removeobserver:self name:uikeyboardwillshownotification object:nil]; [[nsnotificationcenter defaultcenter] removeobserver:self name:uikeyboardwillhidenotification object:nil]; } - (void)keyboardwillhide:(nsnotification *)n { nsdictionary* userinfo = [n userinfo]; cgsize keyboardsize = [[userinfo objectforkey:uikeyboardframebeginuserinfokey] cgrectvalue].size; cgrect viewframe = self.scrollview.frame; viewframe.size.height += (keyboardsize.height - self.toolbar.frame.size.height); [self.scrollview setframe:viewframe]; keyboardisshown = no; } - (void)keyboardwillshow:(nsnotification *)n { if (keyboardisshown) { return; } nsdictionary* userinfo = [n userinfo]; cgsize keyboardsize = [[userinfo objectforkey:uikeyboardframebeginuserinfokey] cgrectvalue].size; cgrect viewframe = self.scrollview.frame; viewframe.size.height -= (keyboardsize.height - self.toolbar.frame.size.height); scrollviewheight = viewframe.size.height; [self.scrollview setframe:viewframe]; keyboardisshown = yes; } for moving between text fields:
-(void)textfielddidbeginediting:(uitextfield *)textfield { [[self savebutton] setenabled:yes]; } -(bool)textfieldshouldreturn:(uitextfield *)textfield { if (textfield == self.label1) { [self.label2 becomefirstresponder]; } else if (textfield == self.label2) { [self.label3 becomefirstresponder]; } else if (textfield == self.label3) { [self.label4 becomefirstresponder]; } else if (textfield == self.label4) { [self.label5 becomefirstresponder]; } else if (textfield == self.label5) { [self.label6 becomefirstresponder]; } else if (textfield == self.label6) { [self.label7 becomefirstresponder]; } else if (textfield == self.label7) { [self.label8 becomefirstresponder]; } else if (textfield == self.label8) { [textfield resignfirstresponder]; } return yes; }
after lots of digging found following code, i've tweaked , tested:
- (void)viewdidload { [super viewdidload]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwasshown:) name:uikeyboarddidshownotification object:nil]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwillbehidden:) name:uikeyboardwillhidenotification object:nil]; keyboardisshown = no; //additional code } - (void)viewdidunload { [[nsnotificationcenter defaultcenter] removeobserver:self name:uikeyboarddidshownotification object:nil]; [[nsnotificationcenter defaultcenter] removeobserver:self name:uikeyboardwillhidenotification object:nil]; } - (void)keyboardwasshown:(nsnotification*)anotification { nsdictionary* info = [anotification userinfo]; cgsize kbsize = [[info objectforkey:uikeyboardframebeginuserinfokey] cgrectvalue].size; uiedgeinsets contentinsets = uiedgeinsetsmake(0.0, 0.0, (kbsize.height - self.toolbar.frame.size.height), 0.0); self.scrollview.contentinset = contentinsets; self.scrollview.scrollindicatorinsets = contentinsets; // if active text field hidden keyboard, scroll it's visible // application might not need or want behavior. cgrect arect = self.view.frame; arect.size.height -= (kbsize.height + (self.toolbar.frame.size.height*2)); if (!cgrectcontainspoint(arect, activefield.frame.origin) ) { cgpoint scrollpoint = cgpointmake(0.0, activefield.frame.origin.y-kbsize.height); [self.scrollview setcontentoffset:scrollpoint animated:yes]; } } // called when uikeyboardwillhidenotification sent - (void)keyboardwillbehidden:(nsnotification*)anotification { uiedgeinsets contentinsets = uiedgeinsetszero; self.scrollview.contentinset = contentinsets; self.scrollview.scrollindicatorinsets = contentinsets; } -(void)textfielddidbeginediting:(uitextfield *)textfield { activefield = textfield; } - (void)textfielddidendediting:(uitextfield *)textfield { activefield = nil; }
Comments
Post a Comment