A.Roe
A.Roe

Reputation: 1095

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 ?

Upvotes: 2

Views: 684

Answers (3)

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52183

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
}

Upvotes: 3

TwoStraws
TwoStraws

Reputation: 13127

Your code won't work because you're trying to add a Double (8.0) to a CGRect (keyboardHeight). As you're using Swift 3 CGRectGetHeight() is unavailable, so you should use this:

saveButtonBottomConstant.constant = keyboardHeight.height + 8.0

Upvotes: 2

J Manuel
J Manuel

Reputation: 3070

You should have either 2 Double values or 2 GCRect values. Change one of them to the other and try again.

Upvotes: 0

Related Questions