活动文本字段上的键盘滚动 - 滚动到视野之外?

Keyboard Scroll on Active Text Field - Scrolling to Out of View?(活动文本字段上的键盘滚动 - 滚动到视野之外?)
本文介绍了活动文本字段上的键盘滚动 - 滚动到视野之外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个应用程序,其视图具有从视图顶部到视图底部的文本字段.我需要在编辑底部字段时滚动它以使字段可见,但它似乎无法正常工作.

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),并将 scrollViewactiveField 插座添加到我的头文件中并链接它们到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.

旧答案

此功能无需注册键盘通知.为了使活动文本字段在键盘上方居中,您只需设置 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 等等.并将代表设置为自己.

谢谢,

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

UIButtons at the bottom of UIScrollView not working(UIScrollView 底部的 UIButtons 不起作用)
Bounds automatically changes on UIScrollView with content insets(UIScrollView 上的边界自动更改,带有内容插图)
iOS5 UITapRecognizer for UIScrollView interfering with buttons. How to fix?(用于 UIScrollView 的 iOS5 UITapRecognizer 干扰按钮.怎么修?)
How to Cancel Scrolling in UIScrollView(如何在 UIScrollView 中取消滚动)
How can i add a activity indicator below the UITableView?(如何在 UITableView 下方添加活动指示器?)
NSURLRequest won#39;t fire while UIScrollView is scrolling(NSURLRequest 在 UIScrollView 滚动时不会触发)