0

我在滚动视图中有一个文本字段和一个文本视图。当我开始编辑 textview 时,视图会适当地向上滚动,因此键盘不会隐藏文本。但是,文本字段不会向上滚动,而是被键盘隐藏。我已将文本字段和文本视图的代表设置为视图控制器。我到目前为止的代码如下。我在这里先向您的帮助表示感谢。

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}

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

- (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)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
kbRect = [self.view convertRect:kbRect toView:nil];

CGSize kbSize = kbRect.size;

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
pageScrollView.contentInset = contentInsets;
pageScrollView.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);
    [pageScrollView setContentOffset:scrollPoint animated:YES];
}
}


- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
pageScrollView.contentInset = contentInsets;
pageScrollView.scrollIndicatorInsets = contentInsets;
}
4

2 回答 2

2

michael tyson 创建了非常易于使用的 TPKeyboardAvoiding,您可以从 github 获得它。https://github.com/michaeltyson/TPKeyboardAvoiding

于 2013-08-12T18:58:27.750 回答
0

我不知道为什么它适用于您的文本视图,但对于带有文本字段的滚动视图,我通过手动执行此操作

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWillBeShown:(NSNotification*)aNotification
{
    if(keyboardShown) return;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];

    NSDictionary* info = [aNotification userInfo];

    // Get the size of the keyboard.
    NSValue* keyboardFrameValue     = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRectWrtScreen    = [keyboardFrameValue CGRectValue];
    CGRect keyboardRectWrtView      = [scrollView convertRect:[[scrollView window] convertRect:keyboardRectWrtScreen fromWindow:nil] fromView: nil];

    float keyboardHeight = keyboardRectWrtView.size.height;

    scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width,scrollView.frame.size.height-keyboardHeight);

    if(activeTextField) {
        CGRect rect = [contentView convertRect:[activeTextField bounds] fromView:activeTextField];
        rect.origin.y -= 25;
        rect.size.height += 50;
        [scrollView scrollRectToVisible:rect animated:YES];
    }

    [UIView commitAnimations];

    keyboardShown = YES;
}

如您所见,我手动将滚动视图移动到可见部分,然后确保文本字段位于滚动视图的中心。

于 2013-08-12T16:09:21.807 回答