Swift 3 NSNotificationCenter KeyboardWill显示/隐藏

时间:2016-06-15 02:52:27

标签: ios swift nsnotificationcenter swift3

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

我有这段代码

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)

与此

配对
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()
    }
}

在第一部分我现在得到一个错误,说“类型'LoginViewController'没有成员'keyboardwillshow / hide'

我不明白为什么它没有看到

下面的方法

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

4 个答案:

答案 0 :(得分:8)

查看更新后的Swift Programming Language book。第1027和1028页是您正在寻找的。它应该是这样的:

func keyboardWillHide(_ notification: NSNotification) {…

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

#selector(LoginViewController.keyboardWillHide(_:))

您还可能需要在课程中添加@objc(keyboardWillHideWithNotification:)

答案 1 :(得分:5)

在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)

答案 2 :(得分:4)

使用可在swift3上运行的代码

您可以使用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)

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

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")
    }
}

答案 3 :(得分:1)

NSNotificationCenter为get show keyboard改变了一些东西:

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)