J'essaye d'exécuter une fonction quand le clavier montre et disparaît et j'ai le code suivant:
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(ViewController.keyBoardUp(Notification :)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
Et la fonction keyBoardUp
ci-dessous:
func keyBoardUp( Notification: NSNotification){
print("HELLO")
}
Cependant, la fonction ne s’imprime pas sur la console lorsque le clavier s’affiche. De l'aide serait grandement appréciée
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: Notification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: Notification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
print("notification: Keyboard will show")
if self.view.frame.Origin.y == 0{
self.view.frame.Origin.y -= keyboardSize.height
}
}
}
func keyboardWillHide(notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.Origin.y != 0 {
self.view.frame.Origin.y += keyboardSize.height
}
}
}
@vandana's answer mis à jour pour refléter modifications en notifications natives dans Swift 4.2.
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func keyboardWillShow(notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
print("notification: Keyboard will show")
if self.view.frame.Origin.y == 0{
self.view.frame.Origin.y -= keyboardSize.height
}
}
}
@objc func keyboardWillHide(notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.Origin.y != 0 {
self.view.frame.Origin.y += keyboardSize.height
}
}
}
De plus, vous devez utiliser UIKeyboardFrameEndUserInfoKey
pour prendre en compte les modifications safeAreaInset
introduites avec iOS 11.
définir l'observateur de notification du clavier dans
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardNotification(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
}
et dans votre fonction le gérer
func keyboardNotification(notification: NSNotification) {
print("keyboard displayed!!")
}
j'espère que ceci vous aidera.
Mis à jour pour Swift:
// MARK:- Kyeboard hide/show methods
func keyboardWasShown(_ notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.Origin.y == 0{
self.view.frame.Origin.y -= keyboardSize.height
}
}
}
func keyboardWillBeHidden(_ notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.Origin.y != 0{
self.view.frame.Origin.y += keyboardSize.height
}
}
}
func registerForKeyboardNotifications(){
//Adding notifies on keyboard appearing
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func deregisterFromKeyboardNotifications(){
//Removing notifies on keyboard appearing
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == mEnterPasswordTextField || textField == mEnterConfirmPassword {
animateViewMoving(up: true, moveValue: 120)
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
if textField == mEnterPasswordTextField || textField == mEnterConfirmPassword {
animateViewMoving(up: false, moveValue: 120)
}
}
func animateViewMoving (up:Bool, moveValue :CGFloat){
let movementDuration:TimeInterval = 0.3
let movement:CGFloat = ( up ? -moveValue : moveValue)
UIView.beginAnimations( "animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration )
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
UIView.commitAnimations()
}
// Dans viewDidLoad ()
self.registerForKeyboardNotifications()
self.deregisterFromKeyboardNotifications()
Swift 4.2
NotificationCenter.default.addObserver(self, selector: #selector(didReceiveKeyboardNotificationObserver(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(didReceiveKeyboardNotificationObserver(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
@objc func didReceiveKeyboardNotificationObserver(_ notification: Notification) {
let userInfo = notification.userInfo
let keyboardBounds = (userInfo!["UIKeyboardBoundsUserInfoKey"] as! NSValue).cgRectValue
let keyboardFrame = (userInfo!["UIKeyboardFrameEndUserInfoKey"] as! NSValue).cgRectValue
let duration = userInfo!["UIKeyboardAnimationDurationUserInfoKey"] as! Double
let curve = userInfo!["UIKeyboardAnimationCurveUserInfoKey"] as! Int
let frameBegin = (userInfo!["UIKeyboardFrameBeginUserInfoKey"] as! NSValue).cgRectValue
let centerBegin = (userInfo!["UIKeyboardCenterBeginUserInfoKey"] as! NSValue).cgPointValue
let center = (userInfo!["UIKeyboardCenterEndUserInfoKey"] as! NSValue).cgPointValue
let location = userInfo!["UIKeyboardIsLocalUserInfoKey"] as! Int
println("keyboardBounds: \(keyboardBounds) \nkeyboardFrame: \(keyboardFrame) \nduration: \(duration) \ncurve: \(curve) \nframeBegin:\(frameBegin) \ncenterBegin:\(centerBegin)\ncenter:\(center)\nlocation:\(location)")
switch notification.name {
case UIResponder.keyboardWillShowNotification:
// keyboardWillShowNotification
case UIResponder.keyboardWillHideNotification:
// keyboardWillHideNotification
default:
break
}
}
Essaye ça
override func viewDidLoad()
{
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
}
@objc func keyboardWillShow(notification: NSNotification) {
if(messageCount > 0)
{
tableView.scrollToRow(at: IndexPath(item:messageCount - 1, section: 0), at: .bottom, animated: true)
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if(messageCount > 0)
{
tableView.scrollToRow(at: IndexPath(item:0, section: 0), at: .top, animated: true)
}
}