Domanda

Questo è un tema abbastanza comune e ha molte risposte là fuori.

Situazione : Ho un (barra degli strumenti meno) UITextView a schermo intero con un UIToolbar in fondo. quando l'UITextView respinge risponditore voglio avere la diapositiva barra degli strumenti con la tastiera e aggiungere un pulsante "fatto" che respingere la tastiera.

Finora : ho questo completamente funzionante, sulla base di questo esempio . Fatta eccezione per il fatto che quando ho messo nel mio [textView becomeFirstResponder]; viewDidLoad poi la barra degli strumenti non è così animato. Anche se keyboardWillShow si chiama. Qualcuno ha qualche idea?

Codice : Solo così si non c'è bisogno di controllare il codice di esempio questo è ciò che sta accadendo:

In 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];
}

In 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];
 }
È stato utile?

Soluzione

Prova a spostare la chiamata -becomeFirstResponder a -viewWillAppear:animated: o -viewDidAppear:animated:. Penso -viewDidLoad viene chiamato di solito poco prima della visione in realtà aggiunto alla gerarchia della vista.

Altri suggerimenti

Aggiungi questo nel codice

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

E il suo tutto fatto ...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top