objective c - Resizing UITextView when keyboard appears in iOS7 -
this question has been asked couple of times, wasn't able find answer...
in ios6 used following resize uitextview whenever keyboard appeared. under ios7 behavior not should (in case, seems nothing resizing @ all). suspect cause auto-layout / constraint behavior of ios7. suggestions? ("notepad" uitextview)?
- (void)keyboardwasshown:(nsnotification*)anotification { nsdictionary* info = [anotification userinfo]; cgsize kbsize = [[info objectforkey:uikeyboardframebeginuserinfokey] cgrectvalue].size; //nslog(@"keyboardsize: %f.%f", kbsize.width, kbsize.height); uiedgeinsets contentinsets = uiedgeinsetsmake(0.0, 0.0, (kbsize.width > kbsize.height ? kbsize.height : kbsize.width), 0); self.notepad.contentinset = contentinsets; self.notepad.scrollindicatorinsets = contentinsets; }
@borana has correct answer, requires tweaking full functionality keyboards.
follow code below:
attach below vertical space - bottom layout guide - textfield
@property (weak, nonatomic) iboutlet nslayoutconstraint *textviewbottomconst; second add observers keyboard notifications.
- (void)observekeyboard { [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwillshow:) name:uikeyboardwillshownotification object:nil]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwillhide:) name:uikeyboardwillhidenotification object:nil]; } add viewdidload
[self observekeyboard]; finally methods handles keyboard changes.
- (void)keyboardwillshow:(nsnotification *)notification { //this make sure keyboard doesnt jump when opening quicktype/emoji or other keyboards. kbheight = 0; height = 0; self.textviewbottomconst.constant = height; self.btnviewbottomconst.constant = height; nsdictionary *info = [notification userinfo]; nsvalue *kbframe = [info objectforkey:uikeyboardframeenduserinfokey]; nstimeinterval animationduration = [[info objectforkey:uikeyboardanimationdurationuserinfokey] doublevalue]; cgrect keyboardframe = [kbframe cgrectvalue]; cgrect finalkeyboardframe = [self.view convertrect:keyboardframe fromview:self.view.window]; int kbheight = finalkeyboardframe.size.height; int height = kbheight + self.textviewbottomconst.constant; self.textviewbottomconst.constant = height; [uiview animatewithduration:animationduration animations:^{ [self.view layoutifneeded]; }]; } - (void)keyboardwillhide:(nsnotification *)notification { nsdictionary *info = [notification userinfo]; nstimeinterval animationduration = [[info objectforkey:uikeyboardanimationdurationuserinfokey] doublevalue]; self.textviewbottomconst.constant = 10; [uiview animatewithduration:animationduration animations:^{ [self.view layoutifneeded]; }]; }
Comments
Post a Comment