2017-10-19 19 views
2

Je suis confronté à une situation très étrange. Je veux faire défiler l'UIScrollView vers visible quand le clavier apparaît sur un UITextView, et cette partie du code fonctionne bien. Mais lorsque le clavier disparaît, la fonction de défilement ne revient pas à sa position d'origine. Quand je le fais glisser, il revient à sa position d'origine. Voici ce que j'ai fait. S'il vous plaît me guider ce que j'ai manquéUIScrollView ne défile pas jusqu'à sa position d'origine lorsque le clavier disparaît

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

Répondre

0

Ok Les gars, j'ai trouvé la solution. En fait, le concept derrière scrollview n'était pas clair pour moi. Mais maintenant, c'est clair et le changement en une seule ligne a fait l'affaire.

- (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

Je vais essayer à l'aveuglette en utilisant UIKeyboardFrameEndUserInfoKey au lieu de UIKeyboardFrameBeginUserInfoKey pour voir si cela fonctionne.

Sinon, je vérifierais si la valeur commentViewFrame est correcte lorsque vous cachez le clavier.

Il y a aussi une autre chose que je ne sais pas si c'est correct. Dans keyboardWillShow, vous référencez self.detailCommentView.frame.origin.y mais dans keyboardWillHide, vous référencez self.dopDetailCommentView.frame.origin.y. Tout va bien?

+0

Mon erreur, le bon est self.detailCommentView.frame.origin.y. Permettez-moi de le modifier maintenant, et merci pour la réponse aussi. Laissez-moi vérifier si cela fonctionne ou non. –

+0

Je n'ai pas compris la première partie de votre solution. Voulez-vous s'il vous plaît partager un code. Merci –

+0

Je voudrais essayer de remplacer 'UIKeyboardFrameBeginUserInfoKey' avec' UIKeyboardFrameEndUserInfoKey' dans votre code et voir si cela fonctionne – RaffAl