无法将类型CGFloat的值赋给NSLayoutConstraint

时间:2016-03-29 16:06:15

标签: ios swift nslayoutconstraint cgfloat

我正在尝试将底部约束设置为键盘的高度(加上4.0以获得一点间距)。但是对于第一位,我收到以下错误;

  

无法将CGFloat类型的值赋给NSLayoutConstraint

我认为它们都是CGFloat值,如何将NSLayoutConstraint值转换为CGFloat? (所以我可以添加间距)

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


}

1 个答案:

答案 0 :(得分:2)

您可以尝试以下代码来更新buttonBottomConstant.constant值,将以下代码放在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)
}