標籤:super ati 是什麼 point efault img png textfield cgpoint
cell中有UITextView時,輸入文字是需要將tableView向上移,基本的做法是,註冊鍵盤變化的通知在通知的方法中做tableVIew的位置調整,
一,一般做法
- (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;
///將內邊距進行調整
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
}
- (void)keyboardWillBeHidden:(NSNotification *)aNotification {
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
}
今天做項目發現以上方法有一定缺陷,當tableView有多組的時候,有一組cell不跟著向上移動照成如下情況:
當鍵盤出現時,尾視圖向上移動了但是這一組中的cell沒有向上移動,這是什麼原因,我至今沒有搞清楚.
上述方法還會出現問題
解決辦法找到了就是在鍵盤出現的通知方法中找到第一響應者(一般是:UITextField),判斷它所在的cell是否顯示在可見地區,如果沒有則讓tableVIew滑動至該cell全部可見:
#pragma mark 鍵盤出現
-(void)keyboardWillShow:(NSNotification *)note
{
CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//取得第一響應者
UIWindow * keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView *activeField = [keyWindow performSelector:@selector(firstResponder)];
//除去鍵盤後的頁面顯示地區
CGRect aRect = self.view.frame;
aRect.size.height = aRect.size.height - keyBoardRect.size.height - 64;
//判斷cell底部是否在顯示範圍內
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 -44);
[_tableView setContentOffset:scrollPoint animated:YES];
}
}
#pragma mark 鍵盤消失
-(void)keyboardWillHide:(NSNotification *)note
{
[UIView animateWithDuration:.35 animations:^{
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 45, 0);
}];
}
這樣就完美解決了以上問題;
當cell中有UItextfiled或者UITextVIew時,彈出鍵盤把tableview往上,但是有的cell沒有移動