键盘滚动活动文本字段 - 滚动到视图外? [英] Keyboard Scroll on Active Text Field - Scrolling to Out of View?

查看:148
本文介绍了键盘滚动活动文本字段 - 滚动到视图外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an application with a view that has text fields from the top of the view to the bottom of the view. I needed it to scroll when editing the bottom fields so that the fields would be visible, but it's not seeming to work correctly.

Apple文档,我将所有代码放入我的程序中(清单4-1,4-2),并将 scrollView activeField 出口添加到我的头文件并链接它们到IB。

Following the Apple docs, I placed all of that code into my program (Listings 4-1, 4-2), and added the scrollView and activeField outlets to my header file and linked them to IB.

问题是,当我单击文本字段时,所有文本字段都会一直在视图之外,直到我关闭键盘。它们向下滚动很远(再次,足够远到没有任何字段可见的地方)。

The problem is as soon as I click into a text field, ALL of the text fields go out of view until I dismiss the keyboard. They scroll down very far (again, far enough to where none of the fields are visible).

有谁知道这个问题可能是由什么引起的?

Does anyone know what that problem could be caused by?

我将代码放在这里Apple Docs让您无需点击即可查看我正在使用的代码。

I'm placing the code in here from the Apple Docs so you can see exactly what code I'm using without having to click away.

//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&中的所有类型的文本字段操作。还有很多。尝试一下,如果任何人在整合方面遇到问题,请告诉我。

UPDATE: Below answer is outdated. For now you can use "TPkeyboardavoiding" library for handle all kind of text field operation in UIScrollView, UITableView & many more. Try it and let me know if any one have a problem in integration.

旧答案

无需为此功能注册键盘通知。为了将活动textField屏幕置于键盘上方,您只需要设置UIscrollView的contentOffset两种方法,如下所示:

There is no need to register for the keyboard notifications for this functionality. In order to center the active textField the screen above the keyboard, you only have to set the contentOffset of the UIscrollView two methods as shown here:

// 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等等没有。并将代表设置为自己。

谢谢,

这篇关于键盘滚动活动文本字段 - 滚动到视图外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆