iOS Development--ui Advanced (vi) keyboard processing

Source: Internet
Author: User

First, keyboard notification
We often need to do certain things when the keyboard pops up or hidden, so we need to listen to the state of the keyboard

When the keyboard state changes, some specific notifications will be sent to the system.
Uikeyboardwillshownotification//keyboard is about to be displayed
Uikeyboarddidshownotification//Keyboard display complete
Uikeyboardwillhidenotification//keyboard is about to be hidden
Uikeyboarddidhidenotification//Keyboard hidden complete
Uikeyboardwillchangeframenotification//Keyboard position size is about to change
Uikeyboarddidchangeframenotification//Keyboard Position size change complete

When the system issues a keyboard notification, it comes with additional keyboard-related information (a dictionary), and the dictionary common key is as follows:
Uikeyboardframebeginuserinfokey//keyboard just started frame
Uikeyboardframeenduserinfokey//keyboard final frame (after the animation has finished executing)
Uikeyboardanimationdurationuserinfokey//Keyboard animation time
Uikeyboardanimationcurveuserinfokey//Keyboard animation execution Rhythm (speed)

When you click the Text field pop-up text, let the text field always follow the keyboard and stick to the keyboard

Just because the keyboard state changes, the system will send some specific notifications, we can monitor the state of the keyboard

-(void) viewdidload {    [super Viewdidload];         Object : nil];} -(void) dealloc{    [[Nsnotificationcenter Defaultcenter] removeobserver:self];}

Implementation method:

/** Called when the frame of the listening keyboard is about to change*/- (void) Keyboardwillchange: (Nsnotification *) note{//get the last frame of the keyboardCGRect frame =[Note.userinfo[uikeyboardframeenduserinfokey] cgrectvalue]; //Modify the bottom constraintSelf.bottomSpace.constant = Self.view.frame.size.height-FRAME.ORIGIN.Y; //Performing animationsCGFloat Duration =[Note.userinfo[uikeyboardanimationdurationuserinfokey] doublevalue]; [UIView animatewithduration:duration animations:^{[Self.view layoutifneeded]; }];} //Touch the screen to exit the keyboard- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{    //text box is no longer the first responder, it exits the keyboard//[Self.textfield Resignfirstresponder]; //[Self.textfield Endediting:yes];[Self.view Endediting:yes];}

UserInfo is a system-provided dictionary property
@property (Nullable, ReadOnly, copy) Nsdictionary *userinfo;
The results are printed as follows:
Uikeyboardanimationcurveuserinfokey = 7; Animation Curve Animation
Uikeyboardanimationdurationuserinfokey = "0.25"; Animation time
Uikeyboardboundsuserinfokey = "Nsrect: {{0, 0}, {320, 253}}"; Keyboard bounds
Uikeyboardcenterbeginuserinfokey = "Nspoint: {160, 694.5}"; Start Center position of keyboard
Uikeyboardcenterenduserinfokey = "Nspoint: {160, 441.5}"; End Center position of keyboard
Uikeyboardframebeginuserinfokey = "Nsrect: {{0, 568}, {320, 253}}";//keyboard Start Popup frame
Uikeyboardframeenduserinfokey = "Nsrect: {{0, 315}, {320, 253}}";//frame to exit the keyboard
Uikeyboardislocaluserinfokey = 1;

iOS Development--ui Advanced (vi) keyboard processing

Related Article

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.