2010-07-01 4 views
1

Ceci est un sujet assez commun et a beaucoup de réponses là-bas.barre d'outils sur le dessus du clavier avec premier répondeur

Situation: J'ai un plein écran (barre d'outils moins) UITextView avec un UIToolbar en bas. Quand UITextView reçoit le premier répondeur, je veux faire glisser la barre d'outils vers le haut avec le clavier et ajouter un bouton "done" qui fermera le clavier.

Jusqu'ici: Je l'ai complètement fonctionnant, basé sur this example. Excepté le fait que lorsque je mets [textView becomeFirstResponder]; dans mon viewDidLoad alors la barre d'outils ne s'anime pas. Même si keyboardWillShow est appelée. Est-ce que quelqu'un a une idée?

code: Juste pour que vous ne devez pas vérifier le code exemple ceci est ce qui se passe:

En viewDidLoad:

- (void)viewDidLoad { 
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; 
    [nc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
    [nc addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 
    [textView becomeFirstResponder]; 
     [super viewDidLoad]; 
} 

En keyboardWillShow:

- (void)keyboardWillShow:(NSNotification *)notification { 
NSLog(@"keyboard will show"); 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]]; 
    [UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]]; 

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                      target:self 
                      action:@selector(keyboardDone)]; 
    NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:[toolbar items]]; 
    [toolbarItems addObject:doneButton]; 
    [toolbar setItems:toolbarItems]; 

    CGRect frame = self.view.frame; 
    frame.size.height -= [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size.height; 
    self.view.frame = frame; 
    [UIView commitAnimations]; 
} 

Répondre

3

Essayez de déplacer l'appel -becomeFirstResponder vers -viewWillAppear:animated: ou -viewDidAppear:animated:. Je pense que -viewDidLoad est généralement appelé juste avant que la vue ne soit réellement ajoutée à la hiérarchie de vue.

+0

Vous êtes incroyable! Je vous remercie. Cela ne fonctionne pas dans -viewWillAppear: animé: mais avec -viewDidAppear: animé. Je ne connaissais pas l'ordre dans lequel ils étaient chargés, alors merci de m'avoir appris quelque chose de nouveau aujourd'hui. – RyanJM

0

Ajouter ceci dans votre code

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { 

    return YES; 
} 

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{ 

    UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init]; 
    keyboardDoneButtonView.barStyle  = UIBarStyleBlack; 
    keyboardDoneButtonView.translucent = YES; 
    keyboardDoneButtonView.tintColor = nil; 
    [keyboardDoneButtonView sizeToFit]; 

    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(doneBtnTapped:)]; 

    // I put the spacers in to push the doneButton to the right side of the picker view 
    UIBarButtonItem *spacer1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                       target:nil action:nil]; 

    // I put the spacers in to push the doneButton to the right side of the picker view 
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                       target:nil action:nil]; 

    [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:spacer, spacer1, doneButton, nil]]; 

    // Plug the keyboardDoneButtonView into the text field... 
    textView.inputAccessoryView = keyboardDoneButtonView; 

    return YES; 
} 

- (void)doneBtnTapped:(id)sender { 
    [yourTextView resignFirstResponder]; 
} 

Et son tout fait ...

Questions connexes