Swift:显示键盘时更改自动布局约束

时间:2016-04-14 18:01:47

标签: ios swift autolayout uitextview uikeyboard

我想在键盘出现时设置UItextView更改约束的底部,以便UItextView被约束"通过textView。我有一个UIView我在UItextView和底部布局指南之间命名了spacer。我试图将spacer的约束值更改为视图底部的键盘高度。任何帮助表示赞赏。

这是我的代码:

@IBOutlet weak var textView: UITextView!    
@IBOutlet weak var spacer: UIView!
@IBOutlet weak var spacerBottomLayoutConstraint: NSLayoutConstraint!
viewDidLoad中的

textView.becomeFirstResponder()

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AdditionalDetailsVC.keyboardShown(_:)), name: UIKeyboardDidShowNotification, object: nil)

魔法发生的地方:

func keyboardShown(notification: NSNotification) {

    let info  = notification.userInfo!

    let value: AnyObject = info[UIKeyboardFrameEndUserInfoKey]!

    let rawFrame = value.CGRectValue

    keyboardFrame = view.convertRect(rawFrame, fromView: nil)

    print(keyboardFrame)

    print(spacerBottomLayoutConstraint)

    //Setting text field to bottom of keyboard
    //spacerBottomLayoutConstraint.constant = keyboardFrame.height

    UIView.animateWithDuration(1, animations: {

        self.spacerBottomLayoutConstraint.constant = self.keyboardFrame.height

        self.view.layoutIfNeeded()
    })
}

2 个答案:

答案 0 :(得分:12)

就个人而言,我发现调整文本视图的底部内容插入更加简单。此代码用于占据整个窗口的文本视图(因此其底部是屏幕的底部);到目前为止还没有下来的文本视图需要更多的基本算术:

func keyboardShown(n:NSNotification) {
    let d = n.userInfo!
    var r = (d[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    r = self.textView.convertRect(r, fromView:nil)
    self.textView.contentInset.bottom = r.size.height
    self.textView.scrollIndicatorInsets.bottom = r.size.height
}

答案 1 :(得分:2)

您可以尝试使用以下代码将spacer的约束值更改为视图底部的键盘高度。将以下代码放在ViewController中。

// 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.
    textView.scrollIndicatorInsets.bottom -= originDelta
    textView.contentInset.bottom -= originDelta

    print(keyboardViewEndFrame)

    spacerBottomLayoutConstraint?.constant = CGFloat(originDelta)

    // Inform the view that its the layout should be updated.
    [self.view 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)
}