键盘滚动活动文本字段 - 滚动到视图外?

时间:2011-08-25 16:16:37

标签: iphone objective-c xcode uiscrollview

我的应用程序的视图中包含从视图顶部到视图底部的文本字段。我需要它在编辑底部字段时滚动,以便字段可见,但它似乎无法正常工作。

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;
}

4 个答案:

答案 0 :(得分:33)

更新:以下答案已过时。现在,您可以使用“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等等。并将代表设置为自我。

谢谢,

答案 1 :(得分:6)

自从我找到它之后,我使用了TPKeyboardAvoiding

效果很好,设置非常简单:

  1. 将UIScrollView添加到视图控制器的xib
  2. 将滚动视图的类设置为TPKeyboardAvoidingScrollView(仍然是 在xib,通过身份检查员)
  3. 将所有控件放在该滚动视图中
  4. 它还会自动连接键盘上的“下一步”按钮以切换文本字段。

    祝你好运!

答案 2 :(得分: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;
}

答案 3 :(得分:0)

在确定代理设置后,我浏览了代码并绘制了所有内容的坐标并完成了所有数学运算并且必须手动设置一些变量,但现在它按预期工作。

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