Ios 移动位于键盘下的表单内容
我使用的是Ios 移动位于键盘下的表单内容,ios,uitableview,uikeyboard,Ios,Uitableview,Uikeyboard,我使用的是UITableView,其中每个单元格都包含一个UITextField,使用苹果官方示例,我希望执行目标,如果活动字段已经隐藏,则将其移动到键盘上方。但是,表单不会移动,尽管UITableView处于滚动状态。这是我的相关代码,请帮助我指出哪些似乎遗漏/错误: @interface ViewController (){ UITextField *activeField; } - (void)viewDidLoad { //.... objTableView.scrol
UITableView
,其中每个单元格都包含一个UITextField
,使用苹果官方示例,我希望执行目标,如果活动字段已经隐藏,则将其移动到键盘上方。但是,表单不会移动,尽管UITableView
处于滚动状态。这是我的相关代码,请帮助我指出哪些似乎遗漏/错误:
@interface ViewController (){
UITextField *activeField;
}
- (void)viewDidLoad
{
//....
objTableView.scrollEnabled=YES;
//....
}
#pragma mark - Keyboard notifications
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (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);
objTableView.contentInset = contentInsets;
objTableView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
[objTableView setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
objTableView.contentInset = contentInsets;
objTableView.scrollIndicatorInsets = contentInsets;
}
#pragma mark - UITextFieldDelegate protocol method
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
请尝试以下代码:
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
CGRect rc = [activeField bounds];
rc = [activeField convertRect:rc toView:objTableView];
[objTableView scrollRectToVisible:rc animated:YES];
}