2017-10-19 8 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

私は解決策を見つけました。実際、スクロールビューの背後にあるコンセプトは私には分かりませんでしたが、今ではそれが明確であり、1行での変更だけがそのトリックを作りました。

- (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の値が正しいかどうかを確認します。

また、正しいかどうかわからないことがもう1つあります。 keyboardWillShowではself.detailCommentView.frame.origin.yを参照していますが、keyboardWillHideではself.dopDetailCommentView.frame.origin.yを参照しています。それは大丈夫ですか?

+0

私のミス、正しいものはself.detailCommentView.frame.origin.yです。私はそれを今編集し、返信にも感謝します。それが動作するかどうかをチェックさせてください。 –

+0

解決策の最初の部分を理解できませんでした。いくつかのコードを教えてください。ありがとう –

+0

あなたのコードで 'UIKeyboardFrameBeginUserInfoKey'を' UIKeyboardFrameEndUserInfoKey'に置き換えてみましょう。 – RaffAl

関連する問題