iOS软键盘遮挡UITableView内文本框问题

1、注册
[objc]  view plain  copy
  1. UIKeyboardDidShowNotification/UIKeyboardDidHideNotification通知。  
  2.   
  3.   
  4. -(id) initWithNibName:(NSString*)nibNameOrNil bundle:nibBundleOrNil {  
  5.     if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {  
  6.   
  7.         // 写在这里,或者viewDidLoad  
  8.         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShown:) name:UIKeyboardDidShowNotification object:nil];  
  9.         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHidden:) name:UIKeyboardDidHideNotification object:nil];  
  10.     }  
  11.     return self;  
  12. }  
  13.   
  14.   
  15. -(void) dealloc {  
  16.     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];  
  17.     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];  
  18. }  


2、当通知到来,调整frame。
[objc]  view plain  copy
  1. -(void) keyboardShown:(NSNotification*) notification {  
  2.     _initialTVHeight = _tableView.frame.size.height;  
  3.   
  4.     CGRect initialFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];  
  5.     CGRect convertedFrame = [self.view convertRect:initialFrame fromView:nil];  
  6.     CGRect tvFrame = _tableView.frame;  
  7.     tvFrame.size.height = convertedFrame.origin.y;  
  8.     _tableView.frame = tvFrame;  
  9. }  
  10.   
  11.   
  12. -(void) keyboardHidden:(NSNotification*) notification {  
  13.     CGRect tvFrame = _tableView.frame;  
  14.     tvFrame.size.height = _initialTVHeight;  
  15.     [UIView beginAnimations:@"TableViewDown" context:NULL];  
  16.     [UIView setAnimationDuration:0.3f];  
  17.     _tableView.frame = tvFrame;  
  18.     [UIView commitAnimations];  
  19. }  



3、触发文本框,滚动tableView
[objc]  view plain  copy
  1. -(void) textFieldDidBeginEditing:(UITextField *)textField {  
  2.    NSIndexPath* path = [NSIndexPath indexPathForRow:row inSection:section];  
  3.    [self performSelector:@selector(scrollToCell:) withObject:path afterDelay:0.6f];  
  4. }  
  5.   
  6. -(void) scrollToCell:(NSIndexPath*) path {  
  7.    [_tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];  
  8. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值