ios - 当键盘出现 Swift 时,使用文本字段和按钮向上移动 View

标签 ios swift xcode

我知道有几个关于这个主题的已发布问题,但没有一个有效。我有一个表格 View ,在它下面有一个包含文本字段和按钮的 UIView。当我的键盘出现时,它覆盖了整个 UIView,使我无法单击“发送”。显示我的应用程序外观的图像:

enter image description here

目前,我的代码是:

 override func viewDidLoad() {
    super.viewDidLoad()
    inputTextField.delegate = self
    inputTextField.layer.cornerRadius = 5
    inputTextField.clipsToBounds = true
    sendButton.layer.cornerRadius = 5
    sendButton.clipsToBounds = true
    self.tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "TableViewCell")
    loadMsg()
    self.hideKeyboardWhenTappedAround()

}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    inputTextField.becomeFirstResponder()
}

当我的键盘出现时,我应该如何将 UIView 连同 UIView 包含的文本字段和按钮一起向上移动?

最佳答案

只需添加 this使用 POD 将库添加到您的项目中,仅此而已。它会自动执行。你不必做任何其他事情。

像那样添加pod

pod 'IQKeyboardManagerSwift'

在 AppDelegate.swift 中,只需导入 IQKeyboardManagerSwift 框架并启用 IQKeyboardManager。

import IQKeyboardManagerSwift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

  IQKeyboardManager.shared.enable = true

  return true
 }
}

作为引用,请查看此网址,https://github.com/hackiftekhar/IQKeyboardManager

关于ios - 当键盘出现 Swift 时,使用文本字段和按钮向上移动 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50325019/

相关文章:

ios - 在 swift3 和 firebase 中将数据发送到服务器

swift - Xcode 8.2 : Unable to load configuration data from specified path/permission error in Mac OSX App:

ios - 在 Swift 3+ 中使用 NSPredicate 对自定义对象进行过滤

objective-c - 在 OS X 上创建和使用静态库

ios - Swift UIRefresh 控件在 UITableView 中拖动刷新导致应用崩溃

c# - WebAPI2 Async ..与所有其他语言兼容吗?

ios - iOS 中另一个应用程序中的应用程序

ios - 从 Parent/Base 到 Child 类的 Swift Type Cast

swift - 如何隐藏搜索栏并使其在按下按钮时显示

ios - NSDateFormatter中的问题将NSDate转换为字符串