iphone - 滚动键盘覆盖的文本字段

标签 iphone ios xcode uiscrollview

我正在按照此文档努力让我的 UIscroll 在键盘阻止文本字段时滚动

http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7

然而遗憾的是...提到了一个变量,activeField,我不知道它是如何声明的。我真的很想知道是否有人可能会建议它是如何/在何处声明的,甚至是在激活键盘时滚动的解决方案会有所帮助。

谢谢

最佳答案

回答您的具体问题,因为 Apple Documentation仅对大小数据使用 activeField,您只需将其声明为私有(private)全局 UIView *activeField,它同样适用于 textFields、textView 等。

但是,他们的代码实际上根本不能很好地工作。我必须对他们的代码进行一些更改才能使我的代码正常工作。这段代码基本上是他们的,只是做了一些小的调整来处理下面列出的所有情况:

1) 如果您在 View 中嵌套了一个较小的 scrollView,而不是全屏 scrollView,它仍然有效。 2) 如果您想将文本字段向下移动到键盘焦点区域,并将它们从键盘后面向上移动。 3) 适用于各种大小的 textViews 和 textFields 4) 如果键盘当前正在显示,它将把所有新点击的字段移动到焦点区域 5) 如果您的内容已经滚动并调用键盘,则可以使用 6) 适用于所有设备的所有键盘尺寸(无硬编码常量)

首先,为这些创建私有(private)变量:

UIView *_activeField;
CGFloat _keyboardHeight;
BOOL _isShowingKeyboard;

接下来,只需将这段代码剪切并粘贴到您的 ViewController 中(看起来很多,但还不错)。

#pragma mark TextFieldKeyboardScrolling

- (void)registerForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)adjustInputFieldsForKeyboard {
    CGFloat keyBoardTopInScrollView = self.view.frame.size.height - _keyboardHeight - self.scrollView.frame.origin.y;
    CGFloat inputFieldBottomInVisibleScrollView = _activeField.frame.origin.y - self.scrollView.contentOffset.y + 30 /* small buffer for cursor size */;
    CGPoint scrollPoint;
    if (keyBoardTopInScrollView > inputFieldBottomInVisibleScrollView) {
        scrollPoint = CGPointMake(0.0, self.scrollView.contentOffset.y - (keyBoardTopInScrollView - inputFieldBottomInVisibleScrollView));
    } else {
        scrollPoint = CGPointMake(0.0, self.scrollView.contentOffset.y + (inputFieldBottomInVisibleScrollView - keyBoardTopInScrollView));
    }
    [self.scrollView setContentOffset:scrollPoint animated:YES];
}

- (void)keyboardWasShown:(NSNotification*)aNotification {
    _isShowingKeyboard = YES;
    NSDictionary* info = [aNotification userInfo];
    _keyboardHeight = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, _keyboardHeight, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
    [self adjustInputFieldsForKeyboard];
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
    _isShowingKeyboard = NO;
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
}

#pragma mark UITextFieldDelegate

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    _activeField = textField;
    if (_isShowingKeyboard) {
        [self adjustInputFieldsForKeyboard];
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    _activeField = nil;
}

就是这样,只需调用 [self registerForKeyboardNotifications];在您的 viewDidLoad 方法中,在 Storyboard中连接您的 scrollView outlet 和 textField/textView 委托(delegate),一切就绪。

关于iphone - 滚动键盘覆盖的文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11890381/

相关文章:

ios 将 View 保留在内存中

iphone - UiImagePickerController takePicture 问题

iphone - 将 PDF 文件导入到应用程序

ios - TableView 单元格在滚动时消失

iphone - 围绕典型用法的核心数据问题

ios - 根据唯一属性 ID 快速添加或更新项目数组

javascript - 让回调在 JavaScript 中同步工作

iphone - 如何使用 PayPalApplicationId

ios - 圆角不适用于除 iPhone XS max 和 XR 以外的所有模拟器

Xcode 11 添加新的约束设置为零 : use set value instead of default/standard