但是,目前在教程中,某些语法已过时。代码基本上应该显示并隐藏用户键盘。我用addObserver
方法得到了一些语法错误,Swift希望我使用关键路径,但是,如果我使用自动'修复 - 它'我得到更多的错误。任何人都可以帮我解决这个问题吗?谢谢!NSNotfication.addObserver - 更新到当前的Swift语法?
NSNotification.addObserver(self, selector: #selector(keyboardwillShow), name: .UIKeyboardWillShow, nil)
NSNotification.addObserver(self, selector: #selector(keyboardwillHide), name: .UIKeyboardWillHide, nil)
func keyboardwillShow(_notification:NSNotification) {
keyboard = (_notification.userInfo![UIKeyboardFrameEndUserInfoKey]! as AnyObject).cgRectValue
UIView.animate(withDuration: 0.4) {
self.scrolledView.frame.size.height = self.scrollViewHeight - self.keyboard.height
}
}
func keyboardwillHide(_notification:NSNotification) {
UIView.animate(withDuration: 0.5) {
self.scrolledView.frame.size.height = self.view.frame.height
}
}
我得到的调试消息:"Incorrect argument labels in call(have _selector:name, expected _forKeyPath:options:context"
您不会将观察者添加到'NSNotification',您应该将它们添加到'NotificationCenter.default' – dan
Isnt Notification Center.default在Swift 3中不可用吗? – Prometheus
它仍然可用 – dan