ios Swift 3 NSNotificationCenter 键盘将显示/隐藏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37825327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:23:11  来源:igfitidea点击:

Swift 3 NSNotificationCenter Keyboardwillshow/hide

iosswiftnsnotificationcenterswift3

提问by RubberDucky4444

I have a piece of code that worked in Swift 2 and I tried using Xcode to update the code to the newest version and I fixed everything except two issues.

我有一段在 Swift 2 中工作的代码,我尝试使用 Xcode 将代码更新到最新版本,除两个问题外,我修复了所有问题。

I have this code :

我有这个代码:

let loginvc: LoginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
NotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)

That pairs along with this:

与此配对:

func keyboardWillShow(notification: NSNotification) {

    constraint.constant = -100
    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

func keyboardWillHide(notification: NSNotification) {

    constraint.constant = 25
    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

On the first part I now get an error saying

在第一部分,我现在收到一条错误消息

Type 'LoginViewController' has no member 'keyboardWillShow/Hide'

类型“LoginViewController”没有成员“keyboardWillShow/Hide”

I don't understand why it is not seeing the method underneath.

我不明白为什么它没有看到下面的方法。

Does anybody know a solution to this issue?

有人知道这个问题的解决方案吗?

采纳答案by Lucas

Check out the updated Swift Programming Language book. Pages 1027 and 1028 are what you're looking for. It should be something like this:

查看更新的Swift 编程语言书籍。第 1027 和 1028 页正是您要查找的内容。它应该是这样的:

func keyboardWillHide(_ notification: NSNotification) {…

Notice the additional underscore above. Also:

请注意上面的附加下划线。还:

#selector(LoginViewController.keyboardWillHide(_:))

You also might need to add @objc(keyboardWillHideWithNotification:)to your class.

您可能还需要添加@objc(keyboardWillHideWithNotification:)到您的班级。

回答by Ricardo Isidro

On Swift 4.2, addObserver name for NSNotificationCenter changed as well:

在 Swift 4.2 上,NSNotificationCenter 的 addObserver 名称也发生了变化:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardDidShowNotification, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardDidHideNotification, object: nil)

回答by Harshad Patel

Use that code that's work on swift3

使用适用于 swift3 的代码

You can use your ViewController (e.g, loginvc) to add notification

您可以使用您的 ViewController(例如,loginvc)来添加通知

let loginvc : LoginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC

    NotificationCenter.default.addObserver(self,
        selector: #selector(loginvc.keyboardWillShow(notification:)),
        name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self,
        selector: #selector(loginvc.keyboardWillHide(notification:)),
        name: NSNotification.Name.UIKeyboardWillHide, object: nil)

Then add keyboard hide and show method

然后添加键盘隐藏和显示方法

func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        print("Show") 
    }
}
func keyboardWillHide(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        print("Hide")
    }
}

回答by Pablo Ruan

NSNotificationCenter have things alter for get show keyboard:

NSNotificationCenter 有一些改变以获得显示键盘:

NotificationCenter.default.addObserver(self, selector: #selector(NovaVisitaVC.abreTeclado(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(NovaVisitaVC.abreTeclado(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)