Two types of custom system pop-up view above the keyboard

Source: Internet
Author: User
We may encounter this in many applications. In the view above the pop-up keyboard, add some controls for auxiliary functions. Below I will introduce them in two cases:
// The physical height of the screen # define screenheight [uiscreen mainscreen]. bounds. size. height // The physical width of the screen # define screenwidth [uiscreen mainscreen]. bounds. size. width @ interface hmtmainviewcontroller () @ property (nonatomic, strong) uiview * testview; @ property (nonatomic, strong) uitextfield * testtextfield; @ property (nonatomic, strong) nsnumber * duration; @ property (nonatomic, strong) nsnumber * curve; @ end @ implementation hmtmainviewcontroller-(ID) initwithnibname :( nsstring *) bundle :( nsbundle *) Detail {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {// custom initialization} return self;}-(void) viewdidload {[Super viewdidload]; // do any additional setup after loading the view. self. navigationitem. title = @ "Demo"; [self testcustonkeyboardview ___ 1]; [self testcustonkeyboardview ___ 2];} // view above the keyboard displayed on the custom system ---> General situation-(void) testcustonkeyboardview ___ 1 {uitextfield * testtextfield = [[uitextfield alloc] initwithframe: cgrectmake (60,200,200, 40)]; testtextfield. borderstyle = uitextborderstyleroundedrect; [self. view addsubview: testtextfield]; // custom view uiview * customview = [[uiview alloc] initwithframe: cgrectmake (0, 0,320, 40)]; customview. backgroundcolor = [uicolor redcolor]; testtextfield. inputaccessoryview = customview; // Add various UI controls to the custom view (taking uibutton as an example) uibutton * button = [[uibutton alloc] initwithframe: cgrectmake (270, 5, 40, 30)]; button. backgroundcolor = [uicolor blackcolor]; [Button settitle: @ "for" forstate: uicontrolstatenormal]; [Button addtarget: Self action: @ selector (didclickbuttonaction) forcontrolevents: uicontroleventtouchupinside]; [customview addsubview: button] ;}- (void) didclickbuttonaction {nslog (@ "% s __% D __|%@" ,__ function __,__ line __, @ "test"); [self. view endediting: Yes];} // view on the top of the keyboard displayed on the custom system, QQ chat, and other effects-(void) testcustonkeyboardview ___ 2 {self. testview = [[uiview alloc] initwithframe: cgrectmake (0, screenheight-40,320, 40)]; _ testview. backgroundcolor = [uicolor graycolor]; [self. view addsubview: _ testview]; self. testtextfield = [[uitextfield alloc] initwithframe: cgrectmake (40, 2,200, 36)]; _ testtextfield. borderstyle = uitextborderstyleroundedrect; [_ testview addsubview: _ testtextfield]; uibutton * leftbtn = [uibutton buttonwithtype: uibuttontypesystem]; leftbtn. frame = cgrectmake (242, 2, 36, 36); [leftbtn settitle: @ "-" forstate: uicontrolstatenormal]; leftbtn. titlelabel. font = [uifont systemfontofsize: 28.0]; [_ testview addsubview: leftbtn]; uibutton * rightbtn = [uibutton buttonwithtype: uibuttontypesystem]; rightbtn. frame = cgrectmake (282, 2, 36, 36); rightbtn. titlelabel. font = [uifont systemfontofsize: 28.0]; [rightbtn settitle: @ "+" forstate: uicontrolstatenormal]; [_ testview addsubview: rightbtn]; // notification-Listen to the keyboard pop-up event [[nsnotificationcenter defacenter center] addobserver: Self selector: @ selector (changekeyboardwillshownotification :) name: uikeyboardwillshownotification object: Nil]; // notification-Listen to the keyboard reclaim event [[nsnotifcenter center defacenter center] addobserver: Self selector: @ selector (changekeyboardwillhidenotification :) name: uikeyboardwillhidenotification object: Nil];}-(void) changekeyboardwillshownotification :( nsnotification *) Notification {nsdictionary * userinfo = [Notification userinfo]; // struct object nsvalue * valueendframe = [userinfo objectforkey: uikeyboardframeenduserinfokey] of the frame after the keyboard pops up; // obtain the Y coordinate cgfloat keyboardendy = valueendframe where the keyboard view is displayed. cgrectvalue. origin. y; // animation time displayed on the keyboard self. duration = [userinfo objectforkey: uikeyboardanimationdurationuserinfokey]; // animation curve self displayed on the keyboard. curve = [userinfo objectforkey: uikeyboardanimationcurveuserinfokey]; // Add a mobile animation to move the view following the keyboard (the animation time and curve are consistent) [uiview animatewithduration: [_ duration doublevalue] animations: ^ {[uiview setanimationbeginsfromcurrentstate: Yes]; // if this parameter is not set, you can see the difference [uiview setanimationcurve: [_ curve intvalue]; _ testview. center = cgpointmake (_ testview. center. x, keyboardendy-_ testview. bounds. size. height/2.0) ;}];/*** + (void) setanimationcurve :( uiviewanimationcurve) curve; * typedef ns_enum (nsinteger, uiviewanimationcurve) {* Fade, // fade in and out, slow at the beginning, from slow to fast, the fastest in the middle, and then slow down * uiviewanimationcurveeasein, // fade in, start slowly and then get faster and faster * uiviewanimationcurveeaseout, // fade out, start fast and then get slower * uiviewanimationcurvelinear // linear speed, start and end is a speed *}; */}-(void) changekeyboardwillhidenotification :( nsnotification *) notification {nsdictionary * userinfo = [Notification userinfo]; // struct object nsvalue * valuestatrframe = [userinfo objectforkey: uikeyboardframeenduserinfokey] of the frame before the keyboard pop-up; // obtain the Y coordinate cgfloat keyboardstatry = valuestatrframe of the keyboard view displayed. cgrectvalue. origin. y; [uiview animatewithduration: [_ duration doublevalue] animations: ^ {[uiview setanimationbeginsfromcurrentstate: Yes]; [uiview setanimation curve: [_ curve intvalue]; _ testview. center = cgpointmake (_ testview. center. x, keyboardstatry-_ testview. bounds. size. height/2.0) ;}] ;}- (void) touchesbegan :( nsset *) touches withevent :( uievent *) event {[self. view endediting: Yes];}


@ Example:




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.