无法使用Swift 4 CGRect高度

时间:2017-06-24 22:11:03

标签: swift cgrect

我正在使用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)

我无法弄明白为什么,任何帮助都非常感激!

2 个答案:

答案 0 :(得分:3)

CGRectkeyboardFrame结构的属性。您正在访问它,就像它是一个类方法。由于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
        })