Je veux déplacer la vue lorsque le keyboard est affiché et vers le bas lorsque le keyboard est caché comme dans un message. Je suis capable de le réaliser, mais je cache la prédiction qu'il y a un espace vide.
Je gère le mouvement du keyboard avec " UIKeyboardWillShowNotification " et " UIKeyboardWillHideNotification "
- (void)viewWillAppear:(BOOL)animated { [[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 { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } #pragma mark - keyboard movements - (void)keyboardWillShow:(NSNotification *)notification { CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; [UIView animateWithDuration:0.3 animations:^{ CGRect f = self.view.frame; f.origin.y = -keyboardSize.height; self.view.frame = f; }]; } -(void)keyboardWillHide:(NSNotification *)notification { [UIView animateWithDuration:0.3 animations:^{ CGRect f = self.view.frame; f.origin.y = 0.0f; self.view.frame = f; }]; }
Je veux gérer le mouvement de la vue comme cela se passe dans les messages app.
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
replace par ci-dessous la ligne
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
Voici ma réalisation:
- (void)viewDidLoad { [super viewDidLoad]; self.defaultViewFrame = self.view.frame; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear: animated]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardHide:) name:UIKeyboardWillHideNotification object:nil]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)onKeyboardShow:(NSNotification *)notification { // method when keyboard appears CGFloat keyboardHeight = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height; [self updateView:YES withKeyboardHeight:keyboardHeight]; } - (void)onKeyboardHide:(NSNotification *)notification { // method when keyboard hides CGFloat keyboardHeight = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height; [self updateView:NO withKeyboardHeight:keyboardHeight]; } ***************************** - (void)updateView:(BOOL)show withKeyboardHeight:(CGFloat)height{ [self.view setFrame:self.defaultViewFrame]; CGRect newViewRect = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - height, self.view.frame.size.width, self.view.frame.size.height); if (show) { [UIView animateWithDuration:0.3 animations:^{ [self.view setFrame:newViewRect]; } completion:^(BOOL finished) {}]; } }