2017-10-19 130 views
2

我正面临着非常奇怪的情况。我想在UITextView出现键盘时将UIScrollView滚动到可见状态,并且这部分代码工作正常。但是当键盘消失时,scrollView不会回到原来的位置。当我拖动它时,它会回到原来的位置。以下是我所做的。请指导我什么,我已经错过了当键盘消失时,UIScrollView不会向下滚动到其原始位置

- (void)keyboardWillHide:(NSNotification *)notification { 
 
    NSDictionary* info = [notification userInfo]; 
 
    kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
 
    CGRect commentViewFrame = self.detailCommentView.frame; 
 
    commentViewFrame.origin.y += kbSize.height; 
 
    
 
    [UIView animateWithDuration:0.3 animations:^{ 
 
     [self.detailCommentView setFrame:commentViewFrame]; 
 
     self.scrollView.contentOffset = CGPointMake(0, self.detailCommentView.frame.origin.y - 90); 
 
    } completion:^(BOOL finished) { 
 
    }]; 
 
} 
 

 
- (void)keyboardWillShow:(NSNotification *)notification { 
 
    NSDictionary* info = [notification userInfo]; 
 
    kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
 
    CGRect commentViewFrame = self.detailCommentView.frame; 
 
    commentViewFrame.origin.y -= kbSize.height; 
 
    [UIView animateWithDuration:0.3 animations:^{ 
 

 
     [self.detailCommentView setFrame:commentViewFrame]; 
 
     self.scrollView.contentOffset = CGPointMake(0, self.detailCommentView.frame.origin.y + 90); 
 
    } completion:^(BOOL finished) { 
 
    }]; 
 
}

回答

0

好的伙计们,我找到了解决方案。实际上,scrollview背后的概念对我来说并不清楚,但现在很明显,一行中的变化只能起到诀窍的作用。

- (void)keyboardWillHide:(NSNotification *)notification { 
 
    NSDictionary* info = [notification userInfo]; 
 
    kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
 
    CGRect commentViewFrame = self.detailCommentView.frame; 
 
    commentViewFrame.origin.y += kbSize.height; 
 
    
 
    [UIView animateWithDuration:0.3 animations:^{ 
 
     [self.detailCommentView setFrame:commentViewFrame]; 
 
     self.scrollView.contentOffset = CGPointMake(0, 0); 
 
    } completion:^(BOOL finished) { 
 
    }]; 
 
}

0

我使用的UIKeyboardFrameEndUserInfoKey代替UIKeyboardFrameBeginUserInfoKey,看看它的工作原理盲目尝试。

否则我会在隐藏键盘时检查commentViewFrame的值是否正确。

还有一件事我不知道是否正确。在keyboardWillShow你参考self.detailCommentView.frame.origin.y,但在keyboardWillHide你引用self.dopDetailCommentView.frame.origin.y。它没事吗?

+0

我的错误,正确的是self.detailCommentView.frame.origin.y。现在让我编辑它,并感谢您的回复。让我检查它是否有效。 –

+0

我无法理解您的解决方案的第一部分。请你分享一些代码。谢谢 –

+0

我试着在你的代码中用'UIKeyboardFrameEndUserInfoKey'替换'UIKeyboardFrameBeginUserInfoKey',看看它是否有效 – RaffAl

相关问题