我正面临着非常奇怪的情况。我想在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) {
}];
}
我的错误,正确的是self.detailCommentView.frame.origin.y。现在让我编辑它,并感谢您的回复。让我检查它是否有效。 –
我无法理解您的解决方案的第一部分。请你分享一些代码。谢谢 –
我试着在你的代码中用'UIKeyboardFrameEndUserInfoKey'替换'UIKeyboardFrameBeginUserInfoKey',看看它是否有效 – RaffAl