【问题标题】:Cannot assign value of type CGFloat to NSLayoutConstraint无法将 CGFloat 类型的值分配给 NSLayoutConstraint
【发布时间】:2016-07-17 07:33:27
【问题描述】:

我正在尝试将底部约束设置为键盘的高度(再加上 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


}

【问题讨论】:

  • 你可能想要buttonBottomConstant.constant = keyboardHeight

标签: ios swift nslayoutconstraint cgfloat


【解决方案1】:

你可以尝试下面的代码来更新 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)
}

【讨论】:

  • 你从来没有回答过这个问题。
【解决方案2】:

Swift 5.5

确保在 NSLayoutConstraint 实例变量上调用属性 constant,对你来说它将是

buttonBottomConstant.constant = 键盘高度

你缺少 .constant

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 2021-04-30
    • 2019-12-03
    • 2017-08-07
    • 2021-03-08
    相关资源
    最近更新 更多