2017-10-13 12 views
0

Le registre du centre de notifications pour l'affichage/masquage du clavier fonctionnait pour mon application. Une fois mis à jour vers iOS 11 ou supérieur, le centre de notification du clavier ne fonctionnait pas?Le centre de notification pour afficher/masquer le clavier ne fonctionnait pas dans iOS 11

func registerNotificationObservers() 
{ 
    NotificationCenter.default.addObserver(self, selector: #selector(ArticleDetailsVC.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 

    NotificationCenter.default.addObserver(self, selector: #selector(ArticleDetailsVC.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 

    } 

func removeNotificationObservers() 
{ 

    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) 

    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) 


} 

func keyboardWillShow(notification: NSNotification) 
{ 
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue 
    { 
     if self.commentsTableView.frame.origin.y == 0{ 
      print("keyboardWillShow ..") 
      self.tableViewFooter.frame.origin.y -= keyboardSize.height - 50 
      self.commentsTableView.frame.origin.y -= keyboardSize.height 

     } 


    } 

} 


func keyboardWillHide(notification: NSNotification) 
{ 
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue 
    { 
     if self.commentsTableView.frame.origin.y != 0{ 

      print("keyboardWillHide ..") 
      self.tableViewFooter.frame.origin.y += keyboardSize.height + 50 
      self.commentsTableView.frame.origin.y += keyboardSize.height 

     } 


    } 
} 

Que devrais-je faire? Merci d'avance.

+0

Afficher le code qui vous êtes à l'aide. –

+0

J'ai ajouté le code – user1553381

+0

, vous devez ajouter @objc devant vos méthodes keyboardWillShow et keyboardWillHide. '@objc func keyboardWillHide (_ notification: Notification)' et '@objc func keyboardWillShow (_ notification: Notification)' –

Répondre

2

Essayez cette syntaxe mise à jour à la place de vos observateurs:

func registerNotificationObservers() { 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil) 
    NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillHide), name: .UIKeyboardWillHide, object: nil) 
} 

func removeNotificationObservers() { 
    NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil) 
    NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil) 
} 

@objc func keyboardWillShow(_ notification: Notification) { 
    print("keyboardWillShow") 
} 


@objc func keyboardWillHide(_ notification: Notification) { 
    print("keyboardWillHide") 
} 
+0

Je tente votre code, ça marche avec moi, le problème est maintenant que le défilement de la table et le pied de page ne fonctionnent pas, je veux dire ce code 'self.tableViewFooter.frame.origin.y - = keyboardSize.height - 50 self.commentsTableView.frame.origin.y - = keyboardSize.height' – user1553381

+0

@ user1553381, c'est pour la logique ne sais pas ce qui a mal tourné là-bas. La question était pour le show/hide. Peut-être pourriez-vous poser une nouvelle question ou ajouter un code plus pertinent. –

+0

ok je vais le faire, ce code fonctionnait bien avec moi dans ios 10, mais une fois que j'ai mis à jour vers ios 11 il ne défile pas bien et je suis sans espoir. – user1553381