如何解决Swift 3中的键盘问题? [英] How to solve keyboard problems in 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:
- 您可以在键盘上以编程方式管理您的框架并显示为@Taichi Kato
- 您可以集成具有相同用途的库。一个这样的库是
IQKeyBoardManager
,其Swift变种为IQKeyboardManagerSwift
您可以在 GitHub 和 cocoapods
- You can manage your frame programmatically on keyboard hide and show like @Taichi Kato
- You can integrate libraries which serves the same purpose. One such library is
IQKeyBoardManager
with its Swift variant asIQKeyboardManagerSwift
You can find it on GitHub and cocoapods
要实现以下步骤:
SWIFT 3
-
只需安装
IQKeyboardManagerSwift
来自Github或cocoapods
Just install
IQKeyboardManagerSwift
from Github or cocoapods
导入 IQKeyboardManagerSwift
in Appdelegate
在中添加以下代码行AppDelegate didFinishLaunchingWithOptions
方法。
IQKeyboardManager.sharedManager().shouldResignOnTouchOutside = true;
Objective-C
- 通过任何媒介安装
IQKeyBoardManager
- 导入
#importIQKeyboardManager.h
inAppdelegate
-
在
AppDelegate didFinishLaunchingWithOptions
方法中添加以下代码行。
- Install
IQKeyBoardManager
via any medium - Import
#import "IQKeyboardManager.h"
inAppdelegate
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屋!