但是,目前在教程中,某些語法已過時。代碼基本上應該顯示並隱藏用戶鍵盤。我用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