A.Roe
A.Roe

Reputation: 1105

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


}

Upvotes: 4

Views: 8197

Answers (2)

Marlhex
Marlhex

Reputation: 1989

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

Upvotes: 3

keshav vishwkarma
keshav vishwkarma

Reputation: 1852

You can try the below code to update buttonBottomConstant.constant value, put the below code in your ViewController.

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    // Listen for changes to keyboard visibility so that we can adjust the text view accordingly.
    let notificationCenter = NSNotificationCenter.defaultCenter()

    notificationCenter.addObserver(self, selector: "handleKeyboardNotification:", name: UIKeyboardWillShowNotification, object: nil)

    notificationCenter.addObserver(self, selector: "handleKeyboardNotification:", name: UIKeyboardWillHideNotification, object: nil)
}

override func viewDidDisappear(animated: Bool) {
    super.viewDidDisappear(animated)

    let notificationCenter = NSNotificationCenter.defaultCenter()

    notificationCenter.removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)

    notificationCenter.removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
// MARK: Keyboard Event Notifications

func handleKeyboardNotification(notification: NSNotification) {
    let userInfo = notification.userInfo!
    print(notification)
    print(notification.object)

    // Get information about the animation.
    let animationDuration: NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue

    let rawAnimationCurveValue = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).unsignedLongValue
    let animationCurve = UIViewAnimationOptions(rawValue: rawAnimationCurveValue)

    // Convert the keyboard frame from screen to view coordinates.
    let keyboardScreenBeginFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
    let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()

    let keyboardViewBeginFrame = view.convertRect(keyboardScreenBeginFrame, fromView: view.window)
    let keyboardViewEndFrame = view.convertRect(keyboardScreenEndFrame, fromView: view.window)
    print(keyboardViewBeginFrame)
    print(keyboardViewEndFrame)

    // Determine how far the keyboard has moved up or down.
    let originDelta = keyboardViewEndFrame.origin.y - keyboardViewBeginFrame.origin.y
    print(originDelta)

    // Adjust the table view's scroll indicator and content insets.
    titleView.scrollIndicatorInsets.bottom -= originDelta
    titleView.contentInset.bottom -= originDelta

    print(keyboardViewEndFrame)

    buttonBottomConstant?.constant = CGFloat(originDelta)

    // Inform the view that its the layout should be updated.
    titleView.setNeedsLayout()

    // Animate updating the view's layout by calling layoutIfNeeded inside a UIView animation block.
    let animationOptions: UIViewAnimationOptions = [animationCurve, .BeginFromCurrentState]
    UIView.animateWithDuration(animationDuration, delay: 0, options: animationOptions, animations: {
        self.view.layoutIfNeeded()
        }, completion: nil)
}

Upvotes: 1

Related Questions