2013-02-28 4 views
0

Alors ... J'ai mon UIScrollView qui monte quand le clavier apparaît, et ça marche ... sauf que UIScrollView et le clavier ne sont pas synchronisés ... d'abord, le clavier apparaît, puis le UIScrollView.Comment retarder l'apparition du clavier iOS?

Je sais qu'il existe un moyen de retarder le clavier afin qu'il apparaisse en même temps que la vue défile vers le haut; comment je fais ça?? J'ai essayé dans viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 

... et j'ai cela aussi:

- (void)keyboardWillShow:(NSNotification *)notification 
{ 
    [NSTimer scheduledTimerWithTimeInterval:4000 target:self selector:@selector(keyboardWillShow:) userInfo:nil repeats:NO]; 
} 

(je sais, 4000 est énorme, mais je voulais assurer qu'il y avait un retard

De plus, quand je ferme le clavier, plutôt que d'utiliser un défilement régulier, l'UIScrollView se remet tout simplement en place au lieu de ralentir ... y a-t-il un moyen raisonnable de s'en occuper?


MISE À JOUR:

Got ... Merci à Steven Fisher pour me aider sur la bonne voie ... je me suis déplacé tout à keyboardWillShow, et a ajouté le code suivant:

[UIScrollView beginAnimations:nil context:NULL]; 
[UIScrollView setAnimationDelegate:self]; 
[UIScrollView setAnimationDuration:.32]; 
[UIScrollView setAnimationBeginsFromCurrentState:NO]; 

D'une certaine manière cela a également résolu mon problème de "saut" quand le clavier disparaît! woohoo!

+1

Votre travail cette arrière. Vous devriez chronométrer les changements de décalage de vues de défilement à l'animation de claviers. Et il est difficile de dire pourquoi il "recule" sans voir de code apparenté. –

+0

Bon, alors comment je fais ça? – ScatteredFrog

Répondre

0

Ceci est le code que la documentation Apple donnée pour cette situation.

// Call this method somewhere in your view controller setup code. 
- (void)registerForKeyboardNotifications 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
     selector:@selector(keyboardWasShown:) 
     name:UIKeyboardDidShowNotification object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
     selector:@selector(keyboardWillBeHidden:) 
     name:UIKeyboardWillHideNotification object:nil]; 

} 



// Called when the UIKeyboardDidShowNotification is sent. 
- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 
    NSDictionary* info = [aNotification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 

    // If active text field is hidden by keyboard, scroll it so it's visible 
    // Your application might not need or want this behavior. 
    CGRect aRect = self.view.frame; 
    aRect.size.height -= kbSize.height; 
    if (!CGRectContainsPoint(aRect, activeField.frame.origin)) { 
    CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height); 
    [scrollView setContentOffset:scrollPoint animated:YES]; 
    } 
    } 



// Called when the UIKeyboardWillHideNotification is sent 
- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 
UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
scrollView.contentInset = contentInsets; 
scrollView.scrollIndicatorInsets = contentInsets; 
} 
+0

C'est EXACTEMENT le code que j'utilise, mais encore une fois, le clavier et la scrollview ne montent pas en même temps: d'abord le clavier, puis le scrollview. Comment puis-je les amener tous les deux à arriver exactement au même moment? – ScatteredFrog

+0

Accédez à 'UIKeyboardWillShowNotification', pas' UIKeyboardDidShowNotification'. –

+0

Aussi: Cela ne faisait pas partie de la question, mais si vous essayez d'éviter le clavier et de supporter la rotation, assurez-vous de ne jamais l'afficher depuis 'viewWillAppear', où les coordonnées de la vue n'ont pas été configurées. Au lieu de cela, utilisez 'dispatch_async (dispatch_get_mainqueue(),^{[field becomeFirstResponder];});' dans 'viewWillAppear'. Cela permettra de s'assurer que les coordonnées ont été mises à jour pour l'orientation actuelle, mais cela sera toujours transparent. (Si vous ne laissez pas l'appareil tourner, ne vous inquiétez pas à ce sujet.) –

0
- (void)viewDidLoad 
    { 

    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWasShown:) 
              name:UIKeyboardDidShowNotification 
              object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWasShown:) 
              name:UIKeyboardWillHideNotification 
              object:nil]; 
} 

- (void)keyboardWasShown:(NSNotification *)notification 
{ 
    // To avoid keyboard hides the view 
    CGRect frame = self.view.bounds; 
if (capitalTextField.enabled ==YES) 
{ 
    if ([notification name]== UIKeyboardDidShowNotification) 
    { 

     frame.origin.y += 200; 
     [self.scrollView scrollRectToVisible:frame animated:YES]; 
    } 
    else 
    { 
     frame.origin.y -= 200; 
     [self.scrollView scrollRectToVisible:frame animated:YES]; 
    } 
    } 
    } 
0

le code ci-dessous retardera pop up

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:0.3f]; 
[textField becomefirstresponder]; 
[UIView commitAnimations];