Swift 3 NSNotificationCenter Keyboardwillshow/hide 翻译为:Swift 3中的NSNotificationCenter键盘将显示/隐藏。

11

我有一段在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()
    }
}

在第一部分,我现在遇到一个错误,提示:

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

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

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


1
LoginViewController.keyboardWillShow(_:) 更改为 LoginViewController.keyboardWillShow(notification:) - JAL
尝试过了,xCode想让我把下划线再加回去。 - RubberDucky4444
1
尝试使用 func keyboardWillHide(_ notification: NSNotification) {#selector(LoginViewController.keyboardWillHide(_:))。请注意,keyboardWillHide 函数中添加了下划线。 - Lucas
1
@RubberDucky4444,请查看更新的Swift编程书籍。第1027页和1028页可能是您要找的内容,您还可能需要在您的类中添加@objc(keyboardWillHideWithNotification:) - Lucas
如果您想将其作为答案,我可以将其标记为正确的。 - RubberDucky4444
显示剩余5条评论
4个回答

10

请查看更新的Swift编程语言书籍。你需要查找的内容在第1027和1028页。大概是这样的:

func keyboardWillHide(_ notification: NSNotification) {…

注意上面多了一个下划线。此外:

#selector(LoginViewController.keyboardWillHide(_:))

你可能还需要在你的类中添加@objc(keyboardWillHideWithNotification:)


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)

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

1

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接