我有一段在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'
我不明白为什么它看不到下面的方法。
有人知道解决这个问题的方法吗?
LoginViewController.keyboardWillShow(_:)
更改为LoginViewController.keyboardWillShow(notification:)
? - JALfunc keyboardWillHide(_ notification: NSNotification) {
和#selector(LoginViewController.keyboardWillHide(_:))
。请注意,keyboardWillHide 函数中添加了下划线。 - Lucas@objc(keyboardWillHideWithNotification:)
。 - Lucas