A.Roe A.Roe - 1 month ago 8x
iOS Question

Binary Operator '+' cannot be applied to type 'CGRect' and 'Double'

I am trying to change the bottom constant of a button when the keyboard appears to the height of the keyboard with an addition 8 points.

However the following

if let keyboardHeight = (n.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {

saveButtonBottomConstant.constant = keyboardHeight + 8.0

gives me the following error in Xcode 8.0 beta 6 when converting to Swift 3

Binary Operator '+' cannot be applied to type 'CGRect' and 'Double'

I understand why this is happening however my attempts have caused more errors than solving the issue.

How can I simply add a Double to a CGRect value in Swift 3 ?


UIKeyboardFrameBeginUserInfoKey returns the keyboard's frame in screen coordinates so keyboardHeight is CGRect type.

You can get the height of keyboard as follows:

if let keyboardFrame = (n.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
  saveButtonBottomConstant.constant = keyboardFrame.height + 8.0