简体   繁体   English

键盘消失时,UIScrollView不会向下滚动到其原始位置

[英]UIScrollView do not scroll down to its original position when keyboard disappear

I am facing very strange situation. 我面临着非常奇怪的情况。 I want to scroll-up the UIScrollView to visible when keyboard appear on some UITextView, and this part of the code is working fine. 当键盘出现在某些UITextView上时,我想向上滚动UIScrollView以使其可见,并且这部分代码可以正常工作。 But when keyboard disappears, scrollView do not come to its original position. 但是,当键盘消失时,scrollView不会回到其原始位置。 When I drag it then it come to its original position. 当我拖动它时,它会回到其原始位置。 Following is what I have done. 以下是我所做的。 Please guide me what I have missed 请指导我错过的一切

- (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) {
    }];
}

I'd blindly try using UIKeyboardFrameEndUserInfoKey instead of UIKeyboardFrameBeginUserInfoKey to see if it works. 我会盲目尝试使用UIKeyboardFrameEndUserInfoKey而不是UIKeyboardFrameBeginUserInfoKey来查看它是否有效。

Otherwise I'd check whether commentViewFrame value is correct when you're hiding the keyboard. 否则,当您隐藏键盘时,我将检查commentViewFrame值是否正确。

There is also one more thing that I don't know whether is correct. 还有一件事我不知道是否正确。 In keyboardWillShow you're referencing self.detailCommentView.frame.origin.y but in keyboardWillHide you're referencing self.dopDetailCommentView.frame.origin.y . keyboardWillShow您引用的是self.detailCommentView.frame.origin.y但在keyboardWillHide您引用的是self.dopDetailCommentView.frame.origin.y Is it alright? 可以吗

I have found the solution. 我找到了解决方案。 Actually the concept behind scrollview was not clear to me. 实际上,scrollview背后的概念对我来说并不明确。 But now it is clear and change in one line only made the trick. 但是现在很明显,只改变一行就可以了。

- (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) {
    }];
}

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

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