为什么视图在键盘存在时为文本视图而不是文本字段时向上滚动?

时间:2013-08-12 15:47:06

标签: iphone xcode keyboard uitextfield

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

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

我不知道为什么它适用于你的textview,但是对于带有textfields的滚动视图我是通过

手动完成的
// 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;
}

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