When there is uitextfiled or Uitextview in the cell, the keyboard pops up tableview, but some cells do not move

Source: Internet
Author: User

When there is uitextview in the cell, the input text is required to move the TableView up, the basic practice is that the notification of the registration of keyboard changes in the method of notification to do tableview position adjustment,

One, general practice

-(void) Registerforkeyboardnotifications {
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardwasshown:) Name: Uikeyboarddidshownotification Object:nil];
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardwillbehidden:) Name: Uikeyboardwillhidenotification Object:nil];
NSLog (@ "register");
}

-(void) Keyboardwasshown: (nsnotification *) anotification {
nsdictionary* info = [Anotification userInfo];
Cgsize kbsize = [[info objectforkey:uikeyboardframebeginuserinfokey] cgrectvalue].size;
Adjust the padding
Uiedgeinsets contentinsets = uiedgeinsetsmake (0.0, 0.0, kbsize.height, 0.0);
}

-(void) Keyboardwillbehidden: (nsnotification *) anotification {

Uiedgeinsets contentinsets = Uiedgeinsetszero;
}
Today, the project found that the above methods have some defects, when the tableview have more than one group, there is a group of cells do not follow the upward movement as follows:

When the keyboard appears, the tail view moves up but the cell in this group does not move up, which is why I haven't figured it out yet.

There are also problems with these methods

The solution found is to find the first responder in the notification method that appears on the keyboard (typically: Uitextfield), to determine if the cell it is in is visible, and if not, let TableView slide to the cell all visible:

#pragma mark keyboard appears

-(void) Keyboardwillshow: (nsnotification *) Note

{

CGRect keyboardrect=[note.userinfo[uikeyboardframeenduserinfokey] cgrectvalue];

Get the first responder

UIWindow * Keywindow = [[uiapplication sharedapplication] Keywindow];  

UIView *activefield = [Keywindow performselector:@selector (FirstResponder)];

Page display area after the keyboard is removed

CGRect arect = self . View. frame;

Arect. size. height = arect. Size. Height-keyboardrect. size. height-up ;

Determine if the cell bottom is within the display range

Cgpoint cellbottompoint = cgpointmake(0.0, Activefield.superview. Superview. Frame. Origin. Y +activefield.superview. Superview. Frame. Size. Height);

if (! Cgrectcontainspoint (Arect, Cellbottompoint)) {

Self . TableView. Contentinset = Uiedgeinsetsmake (0, 0, Keyboardrect. Size. height, 0);

cgpoint scrollpoint = cgpointmake (0.0, cellbottompoint. Y-arect. size. Height----);

[_tableview setcontentoffset:scrollpoint animated:YES];

}

}

#pragma mark keyboard disappears

-(void) Keyboardwillhide: (nsnotification *) Note

{

[UIView animatewithduration:. animations: ^{

Self . TableView. Contentinset = Uiedgeinsetsmake (0, 0, 0);

}];

}

This is the perfect solution to the above problems;

When there is uitextfiled or Uitextview in the cell, the keyboard pops up tableview, but some cells do not move

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.