Scroll to the current text box and press return to hide

Source: Internet
Author: User
-(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.