2016-03-29 47 views
0

Alt kısıtlamayı klavyenin yüksekliğine (ve artı aralık için 4.0) ayarlamaya çalışıyorum. Ancak ilk bit için aşağıdaki hatayı alıyorum;CGFloat türüne değer atanamıyor NSLayoutConstraint

Bir CGFloat için NSLayoutConstraint değerini dönüştürmek nasıl, ben her ikisi de CGFloat değerleri sanıyordum NSLayoutConstraint

tip CGFloat değerini atanamıyor?

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

Muhtemelen 'buttonBottomConstant.constant = keyboardHeight' isteyebilirsiniz. – dan

cevap

1

Sen buttonBottomConstant.constant değerini güncellemek için aşağıdaki kodu deneyin viewController aşağıdaki kodu koyabilirsiniz (o halde ben boşluk ekleyebilirsiniz).

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) 
} 
İlgili konular