-(Void) viewwillappear :( bool) animated {// notice displayed on the registered keyboard [[nsicationicationcenter defaultcenter] addobserver: Self selector: @ selector (keyboarddidshow :) name: Your object: Nil]; // register the keyboard hide notification [[nsnotificationcenter defacenter center] addobserver: Self selector: @ selector (keyboarddidhide :) name: uikeyboarddidhidenotification object: Nil]; [Super viewwillappear: animated];} -(void) viewwilldisapp Ear :( bool) animated {// notice of removing keyboard [[nsnotifcenter center defacenter center] removeobserver: Self name: uikeyboarddidshownotification object: Nil]; // unhide the keyboard notification [[nsnotificationcenter defacenter center] removeobserver: Self name: icationobject: Nil]; [Super viewwilldisappear: animated];}-(void) keyboarddidshow: (nsnotification *) notif {If (keyboardvisible) {return; // when the keyboard appears, ignore the notification} // obtain the keyboard size Nsdictionary * info = [notif userinfo]; nsvalue * avalue = [info objectforkey: uikeyboardframeenduserinfokey]; cgsize keyboardsize = [avalue cgrectvalue]. size; // redefine the srollview size cgrect viewframe = [self. scrollview frame]; viewframe. size. height-= keyboardsize. height; self. scrollview. frame = viewframe; // scroll to the current text box // The frame method can be used to view the struct data of the current control cgrect textfieldrect = [self. textfield frame]; // specifies to scroll to a rectangle. [Self. scrollview scrollrecttovisible: textfieldrect animated: Yes]; keyboardvisible = yes;}-(void) keyboarddidhide: (nsnotification *) notif {// obtain the keyboard size nsdictionary * info = [notif userinfo] nsvalue * avalue = [info objectforkey: uikeyboardframeenduserinfokey]; cgsize keyboardsize = [avalue cgrectvalue]. size; // redefine the srollview size cgrect viewframe = [self. scrollview frame]; viewframe. size. height + = keyb Oardsize. height; self. scrollview. Frame = viewframe; If (! Keyboardvisible) {return;} keyboardvisible = no;}-(bool) textfieldshouldreturn :( uitextfield *) textfield {// when you press return, the keyboard disappears [textfield resignfirstresponder]; return yes ;}
Scroll to the current text box and press return to hide