Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot assign value of type CGFloat to NSLayoutConstraint

I am trying to set the bottom constraint to the height of the keyboard (and plus 4.0 for a bit of spacing). However For the first bit I am getting the following error;

Cannot assign value of type CGFloat to NSLayoutConstraint

I thought they were both CGFloat values, how can I convert the NSLayoutConstraint value to a CGFloat ? (so then I can add spacing)

func keyboardWillShow(n:NSNotification) {
    let d = n.userInfo!
    var r = (d[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    r = self.titleView.convertRect(r, fromView:nil)
    self.titleView.contentInset.bottom = r.size.height
    self.titleView.scrollIndicatorInsets.bottom = r.size.height
    self.keyboardShowing = true

    guard let keyboardHeight = (n.userInfo! as NSDictionary).objectForKey(UIKeyboardFrameBeginUserInfoKey)?.CGRectValue.size.height else {
        return
    }

    buttonBottomConstant = keyboardHeight


}
like image 754
A.Roe Avatar asked Aug 31 '25 00:08

A.Roe


1 Answers

Swift 5.5

Make sure you call the attribute constant on the NSLayoutConstraint instance variable, for you it will be

buttonBottomConstant.constant = keyboardHeight

You were missing .constant

like image 173
Marlhex Avatar answered Sep 02 '25 16:09

Marlhex