轉(zhuǎn)載自:http://blog.csdn.net/yo_yo_yang/article/details/51384421
首先在ios4以后柄瑰,當(dāng)UITableViewCell里有UITextfield,當(dāng)輸入時鍵盤遮蓋了UITextField,UITableView是會自動上移士败,當(dāng)如果要讓tableView自動滾動的話,還需要設(shè)置一下tableView的contentInset褥伴。接下來介紹一下實現(xiàn)步驟谅将,
首先監(jiān)聽鍵盤出現(xiàn)和消失:
//監(jiān)聽鍵盤出現(xiàn)和消失
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];```
收到通知在方法里面實現(xiàn):
pragma mark 鍵盤出現(xiàn)
-(void)keyboardWillShow:(NSNotification *)note
{
CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyBoardRect.size.height, 0);
}
pragma mark 鍵盤消失
-(void)keyboardWillHide:(NSNotification *)note
{
self.tableView.contentInset = UIEdgeInsetsZero;
}
這樣就可以實現(xiàn)自動滾動了,另一種方法是鍵盤出現(xiàn)的時候把tableView的frame的高度減去鍵盤的高度重慢,也可以實現(xiàn)饥臂,例如:
pragma mark 鍵盤出現(xiàn)
-(void)keyboardWillShow:(NSNotification *)note
{
CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
self.tableView.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64 - keyBoardRect.size.height);
}
pragma mark 鍵盤消失
-(void)keyboardWillHide:(NSNotification *)note
{
self.tableView.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64);
}
推薦第一種方法,demo下載地址:(https://github.com/yybchl/yoyo.git)