//自定义键盘key事件
func addCustomKeyboardEvent() {
NSNotificationCenter.defaultCenter().removeObserver(self)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillChange:", name: UIKeyboardWillChangeFrameNotification, object: nil)
}
func keyboardWillChange(sender: NSNotification) {
let _ = UITextInputMode().primaryLanguage
let userInfo: NSDictionary = sender.userInfo!
//let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey]!.doubleValue
let keyboardFrame = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue
//let newsCtr = self.childViewControllers[0] as? NewsTableController
weak var this = self
UIView.animateWithDuration(0.2, animations: { () -> Void in
if keyboardFrame.origin.y > self.view.frame.height {
this?.comment.frame.origin.y = keyboardFrame.origin.y - 41
}else {
this?.comment.frame.origin.y = keyboardFrame.origin.y - 41
}
})
}
注意 监听键盘高度的方法是UIKeyboardWillChangeFrameNotification,不要写成别的 ,萌萌大