在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等等.并将代表设置为自我.
谢谢,
总结以上是内存溢出为你收集整理的iphone – 活动文本字段上的键盘滚动 – 滚动到视图外?全部内容,希望文章能够帮你解决iphone – 活动文本字段上的键盘滚动 – 滚动到视图外?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)