如何解决Swift 3中的键盘问题? [英] How to solve keyboard problems in Swift 3?

查看:545
本文介绍了如何解决Swift 3中的键盘问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题在于,当我尝试在文本字段中写入时,键盘会覆盖它们。如何滚动文本字段以查看我在写什么。我有下面的代码行来启用返回键并在其他地方触摸时隐藏键盘:

The problem is that when I try to write in the text fields the keyboard cover them up. How can I scroll the text field up to see what am I writing. I have below lines of code to enable the return key and to hide the keyboard when you touch in a different place:

override func didReceiveMemoryWarning() {
  super.didReceiveMemoryWarning()
  // Dispose of any resources that can be recreated.

}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    self.view.endEditing(true)
    return false
}


推荐答案


如何向上滚动文本字段以查看我在写什么

How can I scroll the text field up to see what am I writing

这可以通过两种方式实现:

This can be achieved in two ways:


  1. 您可以在键盘上以编程方式管理您的框架并显示为@Taichi Kato

  2. 您可以集成具有相同用途的库。一个这样的库是 IQKeyBoardManager ,其Swift变种为 IQKeyboardManagerSwift 您可以在 GitHub cocoapods

  1. You can manage your frame programmatically on keyboard hide and show like @Taichi Kato
  2. You can integrate libraries which serves the same purpose. One such library is IQKeyBoardManager with its Swift variant as IQKeyboardManagerSwift You can find it on GitHub and cocoapods

要实现以下步骤:

SWIFT 3


  1. 只需安装 IQKeyboardManagerSwift 来自Github或cocoapods

  1. Just install IQKeyboardManagerSwift from Github or cocoapods

导入 IQKeyboardManagerSwift in Appdelegate

中添加以下代码行AppDelegate didFinishLaunchingWithOptions 方法。

IQKeyboardManager.sharedManager().shouldResignOnTouchOutside = true;


Objective-C


  1. 通过任何媒介安装 IQKeyBoardManager

  2. 导入 #importIQKeyboardManager.h in Appdelegate

  3. AppDelegate didFinishLaunchingWithOptions 方法中添加以下代码行。

  1. Install IQKeyBoardManager via any medium
  2. Import #import "IQKeyboardManager.h" in Appdelegate
  3. Add the below lines of code in AppDelegate didFinishLaunchingWithOptions method.

IQKeyboardManager.sharedManager.shouldResignOnTouchOutside = true;


这是完成。这是您需要编写的唯一代码。

And this is Done. This is the only code which you need to write.

这篇关于如何解决Swift 3中的键盘问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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