IOS monitor keyboard height, input box up

Source: Internet
Author: User

1 //set the input box---"because the input box uses the Get method, the first call to the input box is called with self";
2Self.textlab.frame=cgrectmake ( -,420, -, -);3_textlab.layer.bordercolor=[Uicolor Bluecolor]. Cgcolor;4_textlab.layer.borderwidth=0.5f;5_textlab.backgroundcolor=[uicolor colorwithred:0.830Green0.801Blue0.881Alpha1.000];6 7 //Add input box to view8 [Self.view AddSubview:self.textlab];9 Ten //monitor Keyboard, keyboard appears One[[Nsnotificationcenter Defaultcenter]addobserver:self
Selector: @selector (Keyboardwill:)
Name:uikeyboardwillshownotificationObject: nil]; A - //Monitor Keyboard Hide -[[Nsnotificationcenter Defaultcenter]addobserver:self
Selector: @selector (keybaordhide:)
Name:uikeyboardwillhidenotificationObject: nil]; the - //set the tap gesture, click on the blank, end the edit, retract the keyboard -UITapGestureRecognizer *tapp=[[UITapGestureRecognizer alloc]
Initwithtarget:self Action: @selector (tapaction:)];
- //Turn on Interaction +Self.view.userinteractionenabled=YES; - //adding gestures to a view +[Self.view Addgesturerecognizer:tapp];

2, the implementation of the Inside method

1 //Click Gesture Method2-(void) Tapaction: (UITapGestureRecognizer *) Sender3 {4 [Self.view Endediting:yes];5 }6 7 //This method is called when the keyboard appears8-(void) Keyboardwill: (Nsnotification *) Sender9 {Ten     //Get keyboard Height OneNsdictionary *dict=[Sender UserInfo]; ANsvalue *value=[Dict Objectforkey:uikeyboardframeenduserinfokey]; -CGRect Keyboardrect =[value cgrectvalue]; -     intheight=Keyboardrect.size.height; the      -     //If the height of the input box is lower than the height after the keyboard appears, the view rises; -     if((_textlab.frame.size.height + _textlab.frame.origin.y) > (Self.view.frame.size.height-height)) -     { +Self.view.frame = CGRectMake (0, -height, self.view.frame.size.width, self.view.frame.size.height); -     } + } A  at //when the keyboard is hidden, the view returns to its original --(void) Keybaordhide: (Nsnotification *) Sender - { -Self.view.frame = CGRectMake (0,0, Self.view.frame.size.width, self.view.frame.size.height); - } -  in //get method for input box --(Uitextfield *) Textlab to { +     if(_textlab==Nil) -     { the_textlab=[uitextfieldNew]; *     } $     return_textlab;Panax Notoginseng}

prevent keyboard occlusion mode two;

Call the proxy method of the Uitextfield input box <uitextfielddelegate>

1 //When you start editing the input box, the soft keyboard appears, performing this event2-(void) Textfielddidbeginediting: (Uitextfield *) Textlab3 {4     //gets the position information of the current input box5CGRect frame =Textlab.frame;6     //get the distance between the bottom of the keyboard and the top of the keyboard, the height of the keyboard7     intoffset = frame.origin.y + Frame.size.height-(Self.view.frame.size.height-256.0);8     //set the time for the view to rise9Nstimeinterval Animatime =0.30f;Ten     //string arbitrary also line ... One[UIView beginanimations:@"Resizeforkeyboard"Context:nil]; A     //add rise time on view 0.3f - [UIView setanimationduration:animatime]; -      the     //move the y-coordinate of the view up to offset units so that the space below is used for the display of the soft keyboard -     if(Offset >0) -Self.view.frame = CGRectMake (0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height); -     //Submit Animation + [UIView commitanimations]; -}
// when the user presses the return key or presses ENTER, keyboard disappears -(BOOL) Textfieldshouldreturn: (Uitextfield *)    textfield{/ / retract keyboard first responder meaning ...     [TextField resignfirstresponder];     return YES;} // after editing the input box, restore the view to its original state -(void) textfielddidendediting: (Uitextfield *) textfield{    =cgrectmake (00, self.view.frame.size.width, self.view.frame.size.height);}

IOS monitor keyboard height, input box up

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.