UITableView中多個UITextField時UITableView的滾動和鍵盤的隱藏


UITableView中多個UITextField時,編輯textfield 時 UITableView 自動滾動到當前textfield 可見的行即不被鍵盤遮住,及鍵盤的顯示和隱藏的問題

1、 采用 NSNotificationCenter 方式

在 .h 文件中 添加兩個變量后面會用到

int keyboardHeight;
BOOL keyboardIsShowing;

@property (nonatomic, retain) UITextField *currentTextField;

在.m 文件里
@synthesize currentTextField = _currentTextField;

首先,在你的viewWillAppear:訂閱到鍵盤上的通知,讓你知道什么時候鍵盤會顯示和隱藏,系統會告訴你的鍵盤大小,但是不要忘記注銷您的viewWillDisappear:

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

實現方法類似下面這樣你調整你曾經為tableView匹配的鍵盤顯示可視面積的大小。不要忘了執行keyboardWillHide:

-(void) keyboardWillShow:(NSNotification *)note {

CGRect keyboardBounds;

[[note.userInfo valueForKey:UIKeyboardWillShowNotification] getValue: &keyboardBounds];

keyboardHeight = keyboardBounds.size.height;

if (keyboardIsShowing == NO) {

keyboardIsShowing = YES;
CGRect frame = self.view.frame;
frame.size.height -= keyboardHeight;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
self.view.frame = frame;
[UIView commitAnimations];
}
}

- (void)keyboardWillHide:(NSNotification*)notification {

if (!keyboardIsShowing) {
return;
}

NSDictionary* userInfo = [notification userInfo];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
[UIView setAnimationCurve:[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];

//CGRect rect = self.view.bounds;
//self.tableView.frame = CGRectMake(0, 0, 320, 416);
NSValue *value = [userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
CGRect viewFrame = self.view.frame;
viewFrame.size.height +=keyboardSize.height;
keyboardIsShowing = NO;
[UIView commitAnimations];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
self.currentTextField = textField;
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *) [self.tableView viewWithTag:self.currentTextField.tag]];

UITableViewCell *cell = (UITableViewCell *) [textField superview];
indexPath = [self.tableView indexPathForCell:cell];
//[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
//int currentIndex = textField.tag;
CGRect frame = textField.frame;
CGFloat rowHeight = self.tableView.rowHeight;
//下面的代碼只是為了判斷是哪一個textField,可以根據自己的情況進行修改,我為了測試加了7個
if (indexPath.row == 0) {
frame.origin.y += rowHeight * 0;
} else if (indexPath.row==1) {
frame.origin.y += rowHeight * 1;
} else if (indexPath.row == 2) {
frame.origin.y += rowHeight * 2;
} else if (indexPath.row ==3){
frame.origin.y += rowHeight * 3;
}else if(indexPath.row==4)
{
frame.origin.y +=rowHeight *4;
} else if(indexPath.row==5)
{
frame.origin.y +=rowHeight *5;
} else if(indexPath.row==6)
{
frame.origin.y +=rowHeight *6;
}
CGFloat viewHeight = self.tableView.frame.size.height;
CGFloat halfHeight = viewHeight / 2;
CGFloat halfh= frame.origin.y +(textField.frame.size.height / 2);

if(halfh<halfHeight){
frame.origin.y = 0;
frame.size.height =halfh;
}else{
frame.origin.y =halfh;
frame.size.height =halfh;
}
[self.tableView scrollRectToVisible:frame animated:YES ];
}

點擊done(完成)按鈕關閉鍵盤,可以在UIControlEventEditingDidEndOnExit 響應事件

[textField addTarget:self action:@selector(textFieldDoneEditing:) forControlEvents:UIControlEventEditingDidEndOnExit];

-(IBAction)textFieldDoneEditing:(id)sender
{
self.currentTextField = (UITextField *)sender;
[self.currentTextField resignFirstResponder];
[sender resignFirstResponder];
//[self.tableView scrollRectToVisible:self.currentTextField.frame animated:YES];
}

2、采用 scrollToRowAtIndexPath

點擊done(完成)按鈕關閉鍵盤,可以在UIControlEventEditingDidEndOnExit 響應事件

[textField addTarget:self action:@selector(textFieldDoneEditing:)forControlEvents:UIControlEventEditingDidEndOnExit];

-(IBAction)textFieldDoneEditing:(id)sender
{
self.currentTextField = (UITextField *)sender;
[self.currentTextField resignFirstResponder];
[sender resignFirstResponder];
[self.tableView scrollRectToVisible:self.currentTextField.frame animated:YES];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField

{
self.currentTextField = textField;
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *) [self.tableView viewWithTag:self.currentTextField.tag]];

//這里要看textField 是直接加到cell 上的還是加的 cell.contentView上的

//直接加到cell 上

UITableViewCell *cell = (UITableViewCell *) [textField superview];
indexPath = [self.tableView indexPathForCell:cell];

[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM