Text Box in UITableView blocked by iOS Soft Keyboard

Source: Internet
Author: User

Text Box in UITableView blocked by iOS Soft Keyboard

1. Registration
UIKeyboardDidShowNotification/UIKeyboardDidHideNotification notification.

-(Id) initWithNibName :( NSString *) nibNameOrNil bundle: nibBundleOrNil {if (self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]) {// write it here, alternatively, viewDidLoad [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (keyboardShown :) name: jsonobject: nil]; [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (keyboardHidden :) name: jsonobject: nil];} return self;}-(void) dealloc {[[nsicationicationcenter defacenter center] removeObserver: self name: UIKeyboardDidShowNotification object: nil]; [[nsicationcenter center defacenter center] removeObserver: self name: UIKeyboardDidHideNotification object: nil];}

2. Adjust the frame when the notification arrives.

-(void) keyboardShown:(NSNotification*) notification {    _initialTVHeight = _tableView.frame.size.height;    CGRect initialFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];    CGRect convertedFrame = [self.view convertRect:initialFrame fromView:nil];    CGRect tvFrame = _tableView.frame;    tvFrame.size.height = convertedFrame.origin.y;    _tableView.frame = tvFrame;}-(void) keyboardHidden:(NSNotification*) notification {    CGRect tvFrame = _tableView.frame;    tvFrame.size.height = _initialTVHeight;    [UIView beginAnimations:@TableViewDown context:NULL];    [UIView setAnimationDuration:0.3f];    _tableView.frame = tvFrame;    [UIView commitAnimations];}

3. Trigger the text box and scroll to tableView.
-(Void) textFieldDidBeginEditing :( UITextField *) textField {
NSIndexPath * path = [NSIndexPath indexPathForRow: row inSection: section];
[Self defined mselector: @ selector (scrollToCell :) withObject: path afterDelay: 0.5f];
}
-(Void) scrollToCell :( NSIndexPath *) path {
[_ TableView scrollToRowAtIndexPath: path atScrollPosition: UITableViewScrollPositionNone animated: YES];
}

 

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.