<small id='r9Kh7'></small><noframes id='r9Kh7'>

    • <bdo id='r9Kh7'></bdo><ul id='r9Kh7'></ul>
    <tfoot id='r9Kh7'></tfoot><legend id='r9Kh7'><style id='r9Kh7'><dir id='r9Kh7'><q id='r9Kh7'></q></dir></style></legend>
  1. <i id='r9Kh7'><tr id='r9Kh7'><dt id='r9Kh7'><q id='r9Kh7'><span id='r9Kh7'><b id='r9Kh7'><form id='r9Kh7'><ins id='r9Kh7'></ins><ul id='r9Kh7'></ul><sub id='r9Kh7'></sub></form><legend id='r9Kh7'></legend><bdo id='r9Kh7'><pre id='r9Kh7'><center id='r9Kh7'></center></pre></bdo></b><th id='r9Kh7'></th></span></q></dt></tr></i><div id='r9Kh7'><tfoot id='r9Kh7'></tfoot><dl id='r9Kh7'><fieldset id='r9Kh7'></fieldset></dl></div>

      出现键盘时调整屏幕大小

      时间:2023-09-11
      <i id='KF2au'><tr id='KF2au'><dt id='KF2au'><q id='KF2au'><span id='KF2au'><b id='KF2au'><form id='KF2au'><ins id='KF2au'></ins><ul id='KF2au'></ul><sub id='KF2au'></sub></form><legend id='KF2au'></legend><bdo id='KF2au'><pre id='KF2au'><center id='KF2au'></center></pre></bdo></b><th id='KF2au'></th></span></q></dt></tr></i><div id='KF2au'><tfoot id='KF2au'></tfoot><dl id='KF2au'><fieldset id='KF2au'></fieldset></dl></div>
      • <bdo id='KF2au'></bdo><ul id='KF2au'></ul>

        • <small id='KF2au'></small><noframes id='KF2au'>

              <tbody id='KF2au'></tbody>

              <tfoot id='KF2au'></tfoot>
              • <legend id='KF2au'><style id='KF2au'><dir id='KF2au'><q id='KF2au'></q></dir></style></legend>

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

                问题描述

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

                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

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

                上一篇:UITextView 加载时不滚动到顶部 下一篇:iOS 自动布局和 UIToolbar/UIBarButtonItems

                相关文章

                <small id='wq3dZ'></small><noframes id='wq3dZ'>

              • <legend id='wq3dZ'><style id='wq3dZ'><dir id='wq3dZ'><q id='wq3dZ'></q></dir></style></legend>

                    <bdo id='wq3dZ'></bdo><ul id='wq3dZ'></ul>
                    <tfoot id='wq3dZ'></tfoot>
                    <i id='wq3dZ'><tr id='wq3dZ'><dt id='wq3dZ'><q id='wq3dZ'><span id='wq3dZ'><b id='wq3dZ'><form id='wq3dZ'><ins id='wq3dZ'></ins><ul id='wq3dZ'></ul><sub id='wq3dZ'></sub></form><legend id='wq3dZ'></legend><bdo id='wq3dZ'><pre id='wq3dZ'><center id='wq3dZ'></center></pre></bdo></b><th id='wq3dZ'></th></span></q></dt></tr></i><div id='wq3dZ'><tfoot id='wq3dZ'></tfoot><dl id='wq3dZ'><fieldset id='wq3dZ'></fieldset></dl></div>