Réglage de l'interface lorsque le clavier apparaît pour UITextField ou UITextView

StackOverflow https://stackoverflow.com/questions/402658

  •  03-07-2019
  •  | 
  •  

Question

J'ai un tableau avec chaque cellule contenant une étiquette et un champ de texte. Le problème est que lorsque je vais modifier la dernière ligne, le clavier masque la partie inférieure du tableau et je ne vois pas ce qui est tapé. Comment puis-je déplacer mon interface au-dessus du clavier afin de voir ce qui est tapé?

Merci, Mustafa

Était-ce utile?

La solution

Vous souhaitez enregistrer votre viewController pour les événements UIKeyboardDidShowNotification et UIKeyboardWillHideNotification . Lorsque vous les recevez, vous devez ajuster les limites de votre table. le clavier mesure 170 pixels de hauteur. Il vous suffit donc de réduire ou d’agrandir les limites de votre tableau de manière appropriée, et le clavier doit s’ajuster correctement au clavier.

Autres conseils

Ce problème est complexe selon le scénario de votre interface utilisateur. Ici, je vais discuter d'un scénario dans lequel UITextField ou UITextview réside dans un UITableViewCell.

  1. Vous devez utiliser NSNotificationCenter pour détecter l'événement UIKeyboardDidShowNotification. voir http://iosdevelopertips.com/user-interface/adjust -textfield-hidden-by-keyboard.html . Vous devez réduire la taille du cadre UITableView afin qu'elle occupe uniquement la zone d'écran non couverte par le clavier.

    1. Si vous tapez sur une UITableViewCell, le système d'exploitation la positionnera automatiquement dans la zone d'affichage de UITableView. Mais cela ne se produit pas lorsque vous appuyez sur UITextView ou UITableViewCell même s'il réside dans UITableViewCell.

Vous devez appeler

[myTableView selectRowAtIndexPath:self.indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];` 

pour programmer et "tapez" " la cellule.

Si vous implémentez les deux points, vous verrez la position UITextView / Field juste au-dessus du clavier. Gardez à l'esprit que la UITableViewCell où réside la UITableView / Field ne peut pas être plus grande que la " non couvert " surface. Si ce n'est pas le cas pour vous, l'approche est différente, mais je ne discuterai pas ici.

Assurez-vous simplement que vous avez suffisamment d’espace pour le défilement. Parce que, à ma connaissance, l’iPhone ajuste et affiche automatiquement la zone de texte sélectionnée lorsque son clavier apparaît.

Supprimer / commenter les lignes où le rect droit est modifié semble résoudre le problème. Merci.

Code modifié:

# define kOFFSET_FOR_KEYBOARD 150.0     // keyboard is 150 pixels height

// Animate the entire view up or down, to prevent the keyboard from covering the author field.
- (void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

// Make changes to the view's frame inside the animation block. They will be animated instead
// of taking place immediately.
CGRect rect = self.view.frame;
CGRect textViewRect = self.textViewBeingEdited.frame;
CGRect headerViewRect = self.headerView.frame;

if (movedUp) {
    // If moving up, not only decrease the origin but increase the height so the view 
    // covers the entire screen behind the keyboard.
    rect.origin.y -= kOFFSET_FOR_KEYBOARD;
    // rect.size.height += kOFFSET_FOR_KEYBOARD;
} else {
    // If moving down, not only increase the origin but decrease the height.
    rect.origin.y += kOFFSET_FOR_KEYBOARD;
    // rect.size.height -= kOFFSET_FOR_KEYBOARD;
}

self.view.frame = rect;
[UIView commitAnimations];

}

     [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasShown:)
                                                     name:UIKeyboardDidShowNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasHidden:)
                                                     name:UIKeyboardDidHideNotification
                                                   object:nil];
        keyboardVisible = NO;

- (void)keyboardWasShown:(NSNotification *)aNotification {
    if ( keyboardVisible )
        return;

    if( activeTextField != MoneyCollected)
    {
        NSDictionary *info = [aNotification userInfo];
        NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;

        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y -= keyboardSize.height-300;
        frame.size.height += keyboardSize.height-50;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];

        viewMoved = YES;
    }

    keyboardVisible = YES;
}

- (void)keyboardWasHidden:(NSNotification *)aNotification {
    if ( viewMoved ) 
    {
        NSDictionary *info = [aNotification userInfo];
        NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;

        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y += keyboardSize.height-300;
        frame.size.height -= keyboardSize.height-50;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];

        viewMoved = NO;
    }

    keyboardVisible = NO;
}
tabelview.contentInset =  UIEdgeInsetsMake(0, 0, 210, 0);
[tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:your_indexnumber inSection:Your_section]
                 atScrollPosition:UITableViewScrollPositionMiddle animated:NO];

essayez ceci mon codage cela aidera pour vous

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top