let frame = (info[UIKeyboardFrameEndUserInfoKey] as NSValue) as CGRect
不起作用。有谁知道为什么这不能按预期运作?
错误:'NSValue'无法转换为'CGRect'
请记住:
let curve = (info[UIKeyboardAnimationCurveUserInfoKey] as NSValue) as UInt
工作正常。
编辑:即使是Apple文档也表明这是一个小故障,因为Cocoa中的UIKeyboardAnimationCurveUserInfoKey
// NSValue of CGRect
Edit2:调试器将其定义为NSConcreteValue。如何将其转换为CGRect?
答案 0 :(得分:9)
let frame = (info[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue()
答案 1 :(得分:1)
在NSNotificationCenter中使用keyboardFrame高度
override func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(self.keyboardWasShown(_:)),
name: UIKeyboardWillChangeFrameNotification,
object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(self.keyboardWillHide(_:)),
name: UIKeyboardWillHideNotification,
object: nil)
}
func keyboardWasShown(notification: NSNotification) {
let info = notification.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.constBottomLayout.constant = keyboardFrame.size.height
})
}
func keyboardWillHide(sender: NSNotification) {
// self.view.frame.origin.y = 0
self.constBottomLayout.constant = 0
}