iphone – 活动文本字段上的键盘滚动 – 滚动到视图外?
发布时间:2020-12-15 01:45:10 所属栏目:百科 来源:网络整理
导读:我有一个应用程序,其视图具有从视图顶部到视图底部的文本字段.我需要它在编辑底部字段时滚动,以便字段可见,但它似乎无法正常工作. 在Apple docs之后,我将所有代码放入我的程序(清单4-1,4-2),并将scrollView和activeField出口添加到我的头文件并将它们链接到I
我有一个应用程序,其视图具有从视图顶部到视图底部的文本字段.我需要它在编辑底部字段时滚动,以便字段可见,但它似乎无法正常工作.
在Apple docs之后,我将所有代码放入我的程序(清单4-1,4-2),并将scrollView和activeField出口添加到我的头文件并将它们链接到IB. 问题是,当我单击文本字段时,所有文本字段都会退出视图,直到我关闭键盘.它们向下滚动很远(再次,足够远到没有任何字段可见的地方). 有谁知道这个问题可能是由什么造成的? 我将代码放在Apple Docs中,这样您就可以确切地看到我正在使用的代码而无需点击. //my .h IBOutlet UIScrollView *scrollView; IBOutlet UITextField *activeField; //.m // 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); scrollView.contentInset = contentInsets; scrollView.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); [scrollView setContentOffset:scrollPoint animated:YES]; } } // Called when the UIKeyboardWillHideNotification is sent - (void)keyboardWillBeHidden:(NSNotification*)aNotification { UIEdgeInsets contentInsets = UIEdgeInsetsZero; scrollView.contentInset = contentInsets; scrollView.scrollIndicatorInsets = contentInsets; } - (void)textFieldDidBeginEditing:(UITextField *)textField { activeField = textField; } - (void)textFieldDidEndEditing:(UITextField *)textField { activeField = nil; } 解决方法
更新:以下答案已过时.现在您可以使用“
TPkeyboardavoiding”库来处理UIScrollView,UITableView&中的所有类型的文本字段操作.还有很多.尝试一下,让我知道是否有人在集成方面有问题.
老答案 无需为此功能注册键盘通知.为了将活动textField屏幕置于键盘上方,您只需要设置UIscrollView的contentOffset两种方法,如下所示: // called when textField start editting. - (void)textFieldDidBeginEditing:(UITextField *)textField { activeField = textField; [scrollView setContentOffset:CGPointMake(0,textField.center.y-60) animated:YES]; } // called when click on the retun button. - (BOOL)textFieldShouldReturn:(UITextField *)textField { NSInteger nextTag = textField.tag + 1; // Try to find next responder UIResponder *nextResponder = [textField.superview viewWithTag:nextTag]; if (nextResponder) { [scrollview setContentOffset:CGPointMake(0,textField.center.y-60) animated:YES]; // Found next responder,so set it. [nextResponder becomeFirstResponder]; } else { [scrollview setContentOffset:CGPointMake(0,0) animated:YES]; [textField resignFirstResponder]; return YES; } return NO; } 注意:所有TextField都应该有增量标签,如1,2,3等等.并将代表设置为自我. 谢谢, (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |