【问题标题】:Swift: Change autolayout constraints when keyboard is shownSwift:显示键盘时更改自动布局约束
【发布时间】:2016-08-06 10:52:13
【问题描述】:

我试图让UItextView 的底部在出现键盘时更改约束,因此UItextView 受到textView 的“约束”。我有一个UIView 我在UItextView 和底部布局指南之间命名为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()
    })
}

【问题讨论】:

  • 还有什么问题?我看到了你的代码,我看到了你的目标,但我没有看到任何问题。 (顺便说一句,您不需要动画;您可以与键盘一起获得自动动画。)
  • 我希望键盘推动UITextView,并在两者之间保留UIView 间隔的缓冲区。现在文本字段在键盘后面。

标签: ios swift autolayout uitextview uikeyboard


【解决方案1】:

我个人觉得调整文本视图的底部内容插入要简单得多。这段代码是针对一个占据整个窗口的文本视图(所以它的底部是屏幕的底部);没有那么远的文本视图需要更多的基本算术:

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
}

【讨论】:

  • 在 Swift 3 中,CGRectValue()cgRectValue
  • @JasonMoore 在 Swift 3 中,您可以直接转换为 CGRect 并完全跳过 NSValue。代码更新版本在这里:github.com/mattneub/Programming-iOS-Book-Examples/blob/master/…
  • 甜蜜!所以d[UIKeyboardFrameEndUserInfoKey] as! CGRect 给遇到这个问题的其他人。
【解决方案2】:

您可以尝试下面的代码来将间隔的约束值更改为视图底部的键盘高度。将以下代码放入您的 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)
}

【讨论】:

    猜你喜欢
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 2016-09-14
    相关资源
    最近更新 更多