2011-08-25 71 views
7

我有一个视图的应用程序,从视图的顶部到视图的底部都有文本字段。我需要它在编辑底部字段时滚动,以便字段可见,但它似乎不能正常工作。键盘在活动文本字段上滚动 - 滚动到视图外?

Apple docs后,我把所有的代码放到我的程序中(清单4-1,4-2),并将scrollViewactiveField插座添加到我的头文件并将它们链接到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; 
} 
+0

我编辑的苹果代码的拷贝 - 粘贴了您的文章,因为它是值得怀疑的SO具有从苹果授权复制它。我还标示让主持人看一看 - 我不确定官方政策是或应该是什么。 –

+0

@Josh:对于[[合理使用]](http://en.wikipedia.org/wiki/Fair_use)目的而言,少量代码可能是正确的。 –

+0

@罗伯特:好的,谢谢。詹姆斯,我想这意味着如果你愿意,你应该把它放回去。对不起,麻烦了。 –

回答

33

UPDATE:下面的答案是过时的。现在您可以使用“TPkeyboardavoiding”库来处理UIScrollView中所有类型的文本字段操作,UITableView &还有更多。尝试一下,让我知道是否有人在整合方面有问题。

老回答

没有必要为键盘通知此功能寄存器。为了居中键盘在屏幕上方的活跃文本框,你只需要设置的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; 
} 

注:所有文本字段应该有增量标签,如1,2, 3没有。并设置代表自我。

感谢,

+5

如果文本字段不直接包含滚动视图“center.y”将是错误的位置。因此,应该使用类似这样的东西来计算正确的位置:'_scrollView.contentOffset = CGPointMake(0,[_scrollView convertPoint:CGPointZero fromView:textField] .y - 60);' – AndiDog

+0

什么是-60值? –

+0

-60是根据您的视图和键盘大小而变化的偏移量。在我看来,这个价值最适合我。 – MinuMaster

0

确保委托成立后,我通过代码去了,掏出所有的一切的坐标,并做了所有的数学,不得不手动设置一些变数,但现在按预期工作。

如果有人在将来需要帮助,我会很乐意提供帮助。我的Twitter是在我的个人资料中。

+0

嗨,詹姆斯,我遇到了一个问题以及遵循苹果的文档。我的问题是当我点击文本字段时,没有任何反应。滚动条不会显示,也不会滚动。你能帮我一下吗?提前致谢。 – dumbfingers

+0

对不起,反应慢。一直忙于工作。你有一些我可以看的代码吗?如果您使用IB,请确保您的所有插座都已连接好。 – Baub

+0

嗨,詹姆斯,感谢您的回复,我几个小时后才问你,我自己解决了这个问题。不管怎么说,还是要谢谢你!!! – dumbfingers

6

因为我发现,我用TPKeyboardAvoiding

这是伟大的工作,而且非常易于安装:

  1. 添加一个UIScrollView到您的视图控制器的XIB
  2. 设置滚动视图类到TPKeyboardAvoidingScrollView(通过身份检查器在xib中仍为 )
  3. 将所有控件放置在该滚动视图中

它还会自动挂上键盘上的“下一步”按钮以切换文本字段。

祝你好运!

1

@ Minumaster的方法工作正常,但我想建议一个更简单但通用的方法,就像苹果所做的那样,将键盘的高度考虑在内,这对我们在键盘上使用自定义工具栏时非常有帮助。 虽然苹果的做法here已经没有几个问题。

这里是我的方法(稍加修改苹果的方式) -

// 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); 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 
} 

// Called when the UIKeyboardWillHideNotification is sent 
- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 
}