如何在Swift 3中编写键盘通知

我正在尝试将此代码更新为swift 3:

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)` 

到目前为止,我刚刚尝试了编译器提供的自动更正。 这导致代码如下:

 let notificationCenter = NotificationCenter.default() notificationCenter.addObserver(self, selector: Selector(("keyboardWillShow:")), name: NSNotification.Name.UIKeyboardWillShow, object: nil) notificationCenter.addObserver(self, selector: Selector(("keyboardWillHide:")), name: NSNotification.Name.UIKeyboardWillHide, object: nil)` 

不幸的是,这并没有让我走得太远,导致额外的错误。

有人解决了吗?

请注意,我只是在尝试编写通知。 我还没(尝试)修复通知function..谢谢

斯威夫特4

 override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil) } func keyboardWillShow(notification: NSNotification) { print("keyboardWillShow") } func keyboardWillHide(notification: NSNotification){ print("keyboardWillHide") } 

你也可以在这些方法内的代码下面获取键盘信息。

 NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange), name: .UIKeyboardWillChangeFrame, object: nil) . @objc func keyboardWillChange(notification: NSNotification) { let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt let curFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue let targetFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue let deltaY = targetFrame.origin.y - curFrame.origin.y } 

我通过编写这样的代码来解决这个问题

 NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil) 

您可以使用类型检查的#selector(Class.method)对替换已弃用的字符串文字Selector

 let center = NotificationCenter.default center.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: .UIKeyboardWillShow, object: nil) center.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: .UIKeyboardWillHide, object: nil) 

#selector语法更安全,因为Swift能够在编译时检查指定的方法实际存在。

有关Swift选择器的更多信息,请参阅rickster的详细解答 。

在Swift 3.0中

  override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil) } 

键盘显示和隐藏

 func keyboardWillShow(notification: NSNotification) { // Your Code Here } func keyboardWillHide(notification: NSNotification) { //Your Code Here } 

Swift 4.2 Xcode 10(10L213o)

与Swift 3相比的主要变化在UIWindow. keyboardWillShowNotification UIWindow. keyboardWillShowNotificationUIWindow.keyboardWillShowNotification

 let notifier = NotificationCenter.default notifier.addObserver(self, selector: #selector(KeyboardLayoutConstraint.keyboardWillShowNotification(_:)), name: UIWindow.keyboardWillShowNotification, object: nil) notifier.addObserver(self, selector: #selector(KeyboardLayoutConstraint.keyboardWillHideNotification(_:)), name: UIWindow.keyboardWillHideNotification, object: nil) @objc func keyboardWillHideNotification(_ notification: NSNotification) {} @objc func keyboardWillHideNotification(_ notification: NSNotification) {} 

您可以分别在两个版本的Swift上执行键盘通知。

添加Objserver:

 NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: .UIKeyboardWillShow, object: nil) 

调用函数swift 3

 func keyboardDidShow() { print("keyboardDidShow") } 

调用函数在swift 4中

 @objc func keyboardDidShow() { print("keyboardDidShow") } 

对于Swift 4.2 .UIKeyboardWillShow重命名为UIResponder.keyboardWillShowNotification.UIKeyboardWillHide重命名为UIResponder.keyboardWillShowNotification

  NotificationCenter.default.addObserver(self, selector: #selector(NameOfSelector), name: UIResponder.keyboardWillShowNotification , object: nil) NotificationCenter.default.addObserver(self, selector: #selector(NameOfSelector), name: UIResponder.keyboardWillHideNotification , object: nil) @objc func NameOfSelector() { //Actions when notification is received }