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
这篇关于出现键盘时调整屏幕大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:出现键盘时调整屏幕大小
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01