iOS research for multiple Uitextfield keyboard handling

Source: Internet
Author: User

The problem that you often need to consider when using Uitextfield in iOS development is keyboard processing. Sometimes, the keyboard that pops up will overwrite the Uitextfield area, affecting user input. This is the time to move the view up. This time we need to consider two points:1, the time to modify the view coordinates;2, the offset of the top shift is how large. 3,UITableView Set section spacing you don't understand.I do the following according to my actual operation:1, gets the pointer to the Uitextfield being editedA pointer that defines a global UitextfieldUitextfield *temptextfiled;in the Uitextfielddelegate proxy method -(void) textfielddidbeginediting: (Uitextfield *) TextFieldinfixed the value of temptextfiled as the address of the Uitextfield currently being edited. -(void) textfielddidbeginediting: (Uitextfield *) TextField{temptextfiled = TextField;}
2, Configuring keyboard Handling Eventsto implement keyboard monitoring in-(void) Viewdidload:[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (keyboardwillshow:) Name: Uikeyboardwillshownotification Object:nil];[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (keyboardwillhide:) Name: Uikeyboardwillhidenotification Object:nil];implement keyboard display and keyboard hiding methodsget the keyboard height in the keyboard display method and configure the keyboard view displacement "It is worth mentioning that this method will also be executed when the user switches the English IME, so there is no need to worry about a part of the keyboard when switching to the Chinese input method." -(void) Keyboardwillshow: (nsnotification *) notification
{
Nsdictionary * info = [notification UserInfo];
Nsvalue *avalue = [info objectforkey:uikeyboardframeenduserinfokey];
CGRect keyboardrect = [Self.view convertrect:[avalue cgrectvalue] fromview:nil];
Double keyboardheight=keyboardrect.size.height;//the height of the keyboard

NSLog (@ "TextField superview].frame.origin.y =%f", [temptextfiled SUPERVIEW].FRAME.ORIGIN.Y);
NSLog (@ "keyboardheight =%f", keyboardheight);
if (([temptextfiled superview].frame.origin.y + keyboardheight + registertable_cell_heghit) >= ([[UIScreen Mainscreen] bounds].size.height-44))
{
When the edit box is covered by the keyboard, the corresponding displacement is made to the view
CGRect frame = CGRectMake (0, A., [UIScreen mainscreen] bounds].size.height-45);
FRAME.ORIGIN.Y-= [temptextfiled superview].frame.origin.y + keyboardheight + registertable_cell_heghit +20-[[UIScreen Mainscreen] Bounds].size.height + 44;//offset = edit box origin y value + keyboard height + edit box height-screen height
Registertableview.frame=frame;
}
}then implement keyboard-hidden processing:in the Uitextfielddelegate proxy method-(void) textfielddidendediting: (Uitextfield *) Textfieldviewor-(void) Keyboardwillhide: (nsnotification *) Notificationmethod to implement the view reset, the following code:CGRect frame = registertableview.frame;FRAME.ORIGIN.Y = 44;//Modify the origin y coordinate of the view. Registertableview.frame=frame;
3, Remove ListenerRemove Listener in-(void) Viewdiddisappear: (BOOL) animated or Dealloc method[[Nsnotificationcenter Defaultcenter]removeobserver:selfname:uikeyboarddidshownotificationObject:nil];[[Nsnotificationcenter Defaultcenter]removeobserver:selfname:uikeyboarddidhidenotificationObject:nil];In this way, no matter how many uitextfield we have on our interface, just a few simple pieces can be implemented Uitextfield not be covered by the keyboard.
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.