2016-03-29 32 views
0

私はキーボードの高さにボトム拘束を設定しようとしています。しかし、最初のビットについては、私は次のエラーが発生しています。CGFloat型の値をNSLayoutConstraintに割り当てることができません

がNSLayoutConstraint

にタイプCGFloatの値を割り当てることができません私は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 


} 
+4

おそらく 'buttonBottomConstant.constant = keyboardHeight'が必要です。 – dan

答えて

1

あなたのViewControllerに以下のコードを入れて、buttonBottomConstant.constant値を更新するには、以下のコードを試すことができます。

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) 
} 
関連する問題