OGeek|极客世界-中国程序员成长平台

标题: ios - 键盘消失时,UIScrollView 不会向下滚动到其原始位置 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:02
标题: ios - 键盘消失时,UIScrollView 不会向下滚动到其原始位置

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

- (void)keyboardWillHideNSNotification *)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)keyboardWillShowNSNotification *)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) {
    }];
}



Best Answer-推荐答案


我会盲目地尝试使用 UIKeyboardFrameEndUserInfoKey 而不是 UIKeyboardFrameBeginUserInfoKey 来看看它是否有效。

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

还有一件事我不知道是否正确。在 keyboardWillShow 中,您引用的是 self.detailCommentView.frame.origin.y,但在 keyboardWillHide 中,您引用的是 self.dopDetailCommentView。 frame.origin.y。没事吧?

关于ios - 键盘消失时,UIScrollView 不会向下滚动到其原始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46836618/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4