出现键盘时调整屏幕大小 [英] Resize the screen when keyboard appears

查看:16
本文介绍了出现键盘时调整屏幕大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个聊天应用程序.当键盘出现时,我必须移动一个文本字段.我正在使用以下代码执行此操作:

I am building a chat app. I have to move a textfield when keyboard appears. I am doing this with the following code:

func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        if let keyboardSize =  (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            kbHeight = keyboardSize.height
            self.animateTextField(true)
        }
    }
}
func keyboardWillHide(notification: NSNotification) {
    self.animateTextField(false)
}

func animateTextField(up: Bool) {
    var movement = (up ? -kbHeight : kbHeight)

    UIView.animateWithDuration(0.3, animations: {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement)
    })
}

但是当我使用此代码时,第一条消息没有显示.我想我必须调整 tableview 的大小.

But when I use this code, the first messages doesn't show. I guess I have to resize the tableview.

以下是键盘出现之前之后的屏幕截图:

Here are screenshots Before and After the keyboard appears:

我正在使用自动布局.

我该如何解决这个问题?

How can I resolve this problem?

推荐答案

你可以为你的table view创建一个底部自动布局约束的outlet.

You can create an outlet of the bottom auto layout constraint of your table view.

然后只需使用以下代码:

Then simply use this code:

func keyboardWillShow(sender: NSNotification) {
    let info = sender.userInfo!
    var keyboardSize = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height
    bottomConstraint.constant = keyboardSize - bottomLayoutGuide.length

    let duration: TimeInterval = (info[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue

    UIView.animate(withDuration: duration) { self.view.layoutIfNeeded() }
}

func keyboardWillHide(sender: NSNotification) {
    let info = sender.userInfo!
    let duration: TimeInterval = (info[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
    bottomConstraint.constant = 0

    UIView.animate(withDuration: duration) { self.view.layoutIfNeeded() }
}

如果您在创建底部约束时遇到问题:

If you have trouble creating the bottom constraint:

在故事板中

  • 选择您的搜索栏.
  • 在右下角,您会看到 3 个图标.单击看起来像 |-[]-| 的中间那个.
  • 在该弹出窗口的顶部,有 4 个框.在底部输入 0.
  • 已创建约束!

现在您可以将它拖到您的视图控制器中,并将其添加为插座.

Now you can drag it to your view controller and add it as an outlet.

另一种解决方案是设置tableView.contentInset.bottom.但我以前没有这样做过.如果你愿意,我可以试着解释一下.

Another solution is to set the tableView.contentInset.bottom. But I haven't done that before. If you prefer that, I can try to explain it.

使用插图:

func keyboardWillShow(sender: NSNotification) {
    let info = sender.userInfo!
    let keyboardSize = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height

    tableView.contentInset.bottom = keyboardSize
}

func keyboardWillHide(sender: NSNotification) {
    tableView.contentInset.bottom = 0
}

您可以尝试使用此代码设置插图.我自己还没有尝试过,但应该是这样的.

You can try this code for setting the inset. I haven't tried it myself yet, but it should be something like that.

根据 nacho4d 的建议更改了持续时间

Changed the duration with the advice of nacho4d

这篇关于出现键盘时调整屏幕大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆