我正在使用swift 4&收到以下错误:
实例成员'height'不能用于'CGRect'
类型
使用以下代码时:
let userInfo = notification.userInfo!
let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let changeInHeight = (CGRect.height(keyboardFrame) + 40) * (show ? 1 : -1)
我无法弄明白为什么,任何帮助都非常感激!
答案 0 :(得分:3)
CGRect
是keyboardFrame
结构的属性。您正在访问它,就像它是一个类方法。由于CGRect
的类型为keyboardFrame.height
,因此您需要let changeInHeight = (keyboardFrame.height + 40) * (show ? 1 : -1)
。
{{1}}
答案 1 :(得分:0)
var userInfo = notification.userInfo!
let keyboardFrame:CGRect = (userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let animationDurarion = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
let changeInHeight = (keyboardFrame.height + 40) * (show ? 1 : -1)
UIView.animate(withDuration: animationDurarion, animations: { () -> Void in
self.bottomConstraint.constant += changeInHeight
})