iphone - how to scroll down the view when keyboard is hidden -
i have view having text field. configured keyboard have done button hide keyboard. have attached delegate txtfield scroll when keyboard shown. working fine when click on done hide keyboard , view remains scrolled , user has manually scroll back.. idea/pointer how fix this..
below code acheive scrolling functionality..
- (void)viewwillappear:(bool)animated { [super viewdidappear:animated]; keyboardisshown = no; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwillshow:) name:uikeyboardwillshownotification object:self.view.window]; // register keyboard notifications [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwillhide:) name:uikeyboardwillhidenotification object:self.view.window]; } - (void)viewwilldisappear:(bool)animated { [super viewdiddisappear:animated]; [[nsnotificationcenter defaultcenter] removeobserver:self name:uikeyboardwillshownotification object:nil]; [[nsnotificationcenter defaultcenter] removeobserver:self name:uikeyboardwillhidenotification object:nil]; } - (void)keyboardwillshow:(nsnotification *)n { if (keyboardisshown) { return; } nsdictionary* userinfo = [n userinfo]; // size of keyboard cgsize keyboardsize = [[userinfo objectforkey:uikeyboardframebeginuserinfokey] cgrectvalue].size; // resize noteview cgrect viewframe = self.scrollview.frame; // i'm subtracting constant ktabbarheight because uiscrollview offset uitabbar portion of keyboard leftover pass uitabbar obscuring uiscrollview. viewframe.size.height -= (keyboardsize.height - ktabbarheight); [uiview beginanimations:nil context:null]; [uiview setanimationbeginsfromcurrentstate:yes]; // kkeyboardanimationduration using 0.3 [uiview setanimationduration:kkeyboardanimationduration]; [self.scrollview setframe:viewframe]; [uiview commitanimations]; scrollview.contentsize = formview.frame.size; keyboardisshown = yes; } - (void)keyboardwillhide:(nsnotification *)n { nsdictionary* userinfo = [n userinfo]; // size of keyboard cgsize keyboardsize = [[userinfo objectforkey:uikeyboardframebeginuserinfokey] cgrectvalue].size; // resize scrollview cgrect viewframe = self.scrollview.frame; // i'm subtracting constant ktabbarheight because uiscrollview offset uitabbar portion of keyboard leftover pass uitabbar obscuring uiscrollview. viewframe.size.height += (keyboardsize.height - ktabbarheight); [uiview beginanimations:nil context:null]; [uiview setanimationbeginsfromcurrentstate:yes]; // kkeyboardanimationduration using 0.3 [uiview setanimationduration:kkeyboardanimationduration]; [self.scrollview setframe:viewframe]; [uiview commitanimations]; keyboardisshown = no; }
if want scroll top animation.
-(void)keyboardwillhide:(nsnotification *)notification { uiedgeinsets contentinsets = uiedgeinsetszero; cgpoint top = cgpointmake(0, 0); [self.scrollview setcontentoffset:top animated:yes]; self.scrollview.scrollindicatorinsets = contentinsets; } the keyboardisshown instance variable not needed in above implementation
Comments
Post a Comment