'Keyboard and TextField [closed]
From Google Translate:
Hello, when I click on a text field, I made some modifications to a picker view appears.
When I navigate from text field to text field without touching on the "Done" button and I went to a text field that contains a picker view and that, I press done, the scroll view does not return to its original size .
Do you know how to please?
- (void)viewDidLoad
{
[super viewDidLoad];
[ScrollView setContentSize:CGSizeMake(320, 974)];
[ScrollView setScrollEnabled:YES];
//on abonne notre instance aux notifications du clavier lorsqu'il apparaît et lorsqu'il disparait
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)aNotification {
//si le clavier est déjà présent on ne fait rien
if (keyboardIsShown) {
return;
}
if (nom.editing == YES)
{
//on récupère la taille du clavier
NSDictionary* info = [aNotification userInfo];
CGRect _keyboardEndFrame;
[[info valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&_keyboardEndFrame];
CGSize keyboardSize = _keyboardEndFrame.size;
//on crée une nouvelle frame pour la scrollView
CGRect viewFrame = self.ScrollView.frame;
viewFrame.size.height -= keyboardSize.height;
//on redimensionne la scrollView dans une animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[self.ScrollView setFrame:viewFrame];
[UIView commitAnimations];
//on scroll jusqu'au champs texte en cours d'édition
CGRect textFieldRect = nom.frame;
[self.ScrollView scrollRectToVisible:textFieldRect animated:YES];
//on enregistre l'état actuel du clavier
keyboardIsShown = YES;
}
else if (dateVol.editing == YES)
{
//on récupère la taille du clavier
NSDictionary* info = [aNotification userInfo];
CGRect _keyboardEndFrame;
[[info valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&_keyboardEndFrame];
CGSize keyboardSize = _keyboardEndFrame.size;
//on crée une nouvelle frame pour la scrollView
CGRect viewFrame = self.ScrollView.frame;
viewFrame.size.height -= keyboardSize.height;
//on redimensionne la scrollView dans une animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[self.ScrollView setFrame:viewFrame];
[UIView commitAnimations];
//on scroll jusqu'au champs texte en cours d'édition
CGRect textFieldRect = prenom.frame;
[self.ScrollView scrollRectToVisible:textFieldRect animated:YES];
//on enregistre l'état actuel du clavier
keyboardIsShown = YES;
}
}
//méthode appelée lorsque le clavier disparaît (on quitte l'édition d'un champs texte)
- (void)keyboardWillHide:(NSNotification *)aNotification {
if (nom.editing == YES || dateVol.editing == YES)
{
//get the size of the keyboard
NSDictionary* info = [aNotification userInfo];
CGRect _keyboardEndFrame;
[[info valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&_keyboardEndFrame];
CGSize keyboardSize = _keyboardEndFrame.size;
//resize the scroll view
CGRect viewFrame = self.ScrollView.frame;
viewFrame.size.height += keyboardSize.height;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[self.ScrollView setFrame:viewFrame];
[UIView commitAnimations];
keyboardIsShown = NO;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
