简体   繁体   English

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

[英]Why does the view scroll up when keyboard is present for a textview but not for a textfield?

I have a textfield and a textview in a scroll view. 我在滚动视图中有一个文本字段和一个textview。 When I begin to edit the textview the view scrolls up appropriately so the keyboard does not hide the text. 当我开始编辑textview时,视图会相应向上滚动,因此键盘不会隐藏文本。 The textfield, however, does not scroll up but is hidden by the keyboard. 但是,文本字段不会向上滚动,而是由键盘隐藏。 I have set the delegates of the textfields and textviews to the view controllers. 我已将textfields和textviews的代理设置为视图控制器。 The code I have so far is below. 我到目前为止的代码如下。 Thanks for the help in advance. 我在这里先向您的帮助表示感谢。

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

michael tyson created TPKeyboardAvoiding which is very easy to use you can get it from github. michael tyson创建了TPKeyboardAvoiding,非常容易使用,你可以从github获得它。 https://github.com/michaeltyson/TPKeyboardAvoiding https://github.com/michaeltyson/TPKeyboardAvoiding

I do not know why it works for your textview, but for a scrollview with textfields I am doing this manually via 我不知道为什么它适用于你的textview,但对于带有textfields的scrollview,我是通过手动完成的

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

as you see, I am manually moving the scroll view to the visible part, then ensuring that the text field is in the center of the scroll view. 如您所见,我手动将滚动视图移动到可见部分,然后确保文本字段位于滚动视图的中心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM