2013-08-14 10 views
16

Come spostare UITextView Su e giù quando si inizia a immettere un valore. Come in TextField, usiamo il suo metodo delegate. Cosa fare in caso di UITextView?Come far muovere UITextView quando è presente la tastiera

+0

'textFieldDidBecomeActive'> sposta di conseguenza il campo di testo. –

+0

http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present vedere questo link –

+1

La ricerca è così difficile da usare? Anche se lo è, guarda la barra laterale: ti mostra domande correlate. – Abizern

risposta

19

Durante la modifica completa View si muoverà su e dopo la modifica fatto si sposterà verso il basso ...

- (void)textViewDidBeginEditing:(UITextView *)textView 
{ 
    [self animateTextView: YES]; 
} 

- (void)textViewDidEndEditing:(UITextView *)textView 
{ 
    [self animateTextView:NO]; 
    } 

- (void) animateTextView:(BOOL) up 
    { 
     const int movementDistance =heightKeyboard; // tweak as needed 
     const float movementDuration = 0.3f; // tweak as needed 
     int movement= movement = (up ? -movementDistance : movementDistance); 
     NSLog(@"%d",movement); 

     [UIView beginAnimations: @"anim" context: nil]; 
     [UIView setAnimationBeginsFromCurrentState: YES]; 
     [UIView setAnimationDuration: movementDuration]; 
     self.view.frame = CGRectOffset(self.inputView.frame, 0, movement); 
     [UIView commitAnimations]; 
    } 

Spero che questo vi aiuterà a ...

+2

Hum, dovresti usare il blocco per l'animazione, il tuo metodo è sconsigliato da iOS4 – Beuj

+1

ho dovuto affrontare molti problemi con questo codice. risolto sostituendo 'self.inputView.frame' con 'self.view.frame'. – Rajesh

+0

Quando non è possibile utilizzare la soluzione con scrollview, questo è il modo più semplice e migliore –

2
#define kOFFSET_FOR_KEYBOARD 80.0 

-(void)keyboardWillShow { 
    // Animate the current view out of the way 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)keyboardWillHide { 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)textFieldDidBeginEditing:(UITextField *)sender 
{ 
    if ([sender isEqual:mailTf]) 
    { 
     //move the main view, so that the keyboard does not hide it. 
     if (self.view.frame.origin.y >= 0) 
     { 
      [self setViewMovedUp:YES]; 
     } 
    } 
} 

//method to move the view up/down whenever the keyboard is shown/dismissed 
-(void)setViewMovedUp:(BOOL)movedUp 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view 

    CGRect rect = self.view.frame; 
    if (movedUp) 
    { 
     // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
     // 2. increase the size of the view so that the area behind the keyboard is covered up. 
     rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
     rect.size.height += kOFFSET_FOR_KEYBOARD; 
    } 
    else 
    { 
     // revert back to the normal state. 
     rect.origin.y += kOFFSET_FOR_KEYBOARD; 
     rect.size.height -= kOFFSET_FOR_KEYBOARD; 
    } 
    self.view.frame = rect; 

    [UIView commitAnimations]; 
} 


- (void)viewWillAppear:(BOOL)animated 
{ 
    // register for keyboard notifications 
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillShow) 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

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

- (void)viewWillDisappear:(BOOL)animated 
{ 
    // unregister for keyboard notifications while not visible. 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self 
              name:UIKeyboardWillHideNotification 
              object:nil]; 
} 

provare questo codice .....

+0

c'è già una domanda sulla stessa cosa .. –

+2

Annnnnddd dovresti usare il blocco per l'animazione, il tuo metodo è sconsigliato da iOS4 ... – Beuj

+0

Questa è la risposta per textField, non textView. – Shmidt

3

ho fatto cambiando il telaio.

-(void)textViewDidBeginEditing:(UITextView *)textView{ 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5]; 
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:viewMsg cache:YES]; 
viewMsg.frame = CGRectMake(10, 50, 300, 200); 
[UIView commitAnimations]; 

NSLog(@"Started editing target!"); 

} 

-(void)textViewDidEndEditing:(UITextView *)textView 
{ 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5]; 
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:viewMsg cache:YES]; 
viewMsg.frame = CGRectMake(10, 150, 300, 200); 
[UIView commitAnimations]; 
} 
5

Ecco un esempio di codice che gestirà automaticamente la tastiera per voi. Keyboard avoiding

Se si utilizza TableView allora il vostro tableView deve essere una sottoclasse di TPKeyboardAvoidingTableView e se si utilizza ScrollView allora il vostro ScrollView deve essere una sottoclasse di TPKeyboardAvoidingScrollView. E questa libreria gestirà automaticamente la tastiera per te.

4

vi consiglio di dare un'occhiata a this guide

in particolare nella sezione: Spostamento di contenuto che si trova sotto la tastiera. Ho usato questo approccio con successo diverse volte.