出现键盘时调整屏幕大小

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

问题描述

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

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

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)