HKUIKeyboardManager
Harrison Kong 的 UI Keyboard Manager
版本历史
1.0.0 - 2020/01/18 - 初次发布
1.1.0 - 2020/08/24 - 添加 unregisterEditableField() 和文档更新
最低要求
Swift 5.0 或更高版本
iOS 13.0 或更高版本
这是什么?
可用功能(可定制)
- 用户点击设计文本域/文本区域外部时,自动隐藏虚拟键盘
- 用户在虚拟键盘上点击回车键时,自动隐藏虚拟键盘
- 用户在外部设计文本域/文本区域进行以下操作之一时,自动隐藏虚拟键盘:点击、捏合、滑动、旋转
- 设备旋转时,自动隐藏虚拟键盘
- 通过转换(segue)打开另一个视图时,自动隐藏虚拟键盘
- 在可滚动视图中:自动调整视图内容大小,适应虚拟键盘的高度,以确保底部内容不被键盘隐藏
- 在可滚动视图中:当点击某处时,将指定文本域/文本区域滚动到视图。此功能确保文本域/文本区域的顶部可见,而不仅仅是底部
在移动应用程序开发中隐藏和显示虚拟键盘一直是一个头疼的问题。需要太多代码,并且在项目中的不同位置,而且您需要在每个项目中这样做。
此键盘管理器将帮助您,提供许多自定义其功能的方式。仅几行代码就可以为您在需要文本输入的每个项目中节省数小时的时间。它还提供了一些额外功能。
要观看其实际效果并尝试其选项,请访问https://github.com/harrisonkong/HKKBMDemo 以浏览此演示应用程序。
以下图表解释了其工作原理。
有两个类,HKUIKeyboardManager
用于不滚动的 UIView
。对于这个版本,它只是隐藏和显示键盘。
另一个类 HKUIKeyboardManagerScrollable
用于 UIScrollView
及其子类,例如 UICollectionView
和 UITableView
。对于这个版本,它也能够在点击时将活动文本字段滚入更好的视图,并且在其背后添加内边距,以便用户可以滚动查看键盘后面的内容。
它通过跟踪一个你注册给它的 UITextFields 或 UITextViews(或其子类)列表来工作。它还使用一些手势识别器来捕获点击和其他手势(它们不会干扰代码中可能需要使用的其他手势识别器)。当检测到在任文本字段和视图外部点击时,它能够通知所有可编辑字段结束编辑,从而隐藏键盘。在单行文本字段(UITextFields)上按下回车键也会隐藏键盘。
此外,对于可滚动的视图版本,它还具有一个选项(默认启用)在点击时将活动文本字段滚动到最佳视图。
如何安装
使用以下方法之一:
1. 使用 CocoaPod
该模块可以通过 CocoaPods Trunk 获得。下面是一个示例 podfile。如果您已经在项目中使用了 podfile,只需添加一行指定 HKUIKeyboardManager
的代码。其他依赖也会自动安装到 xcworkspace 中。
platform :ios, '13.0'
target 'MyTarget' do
use_frameworks!
pod 'HKUIKeyboardManager', '~> 1.0.0'
# other pods for your project
end
如果您使用的是 CocoaPod 选项,不要忘记先导入它
import HKUIKeyboardManager
2. 通过包含源文件
您也可以简单地将以下源文件包含到项目中
HKUIKeyboardManager.swift
HKUIKeyboardManagerScrollable.swift
UIView+HKUtilities.swift
如何使用它
1. 初始化和注册文本字段
var kbManager : HKUIKeyboardManagerScrollable?
.
.
.
override func viewDidLoad() {
super.viewDidLoad()
// code ...
kbManager = HKUIKeyboardManager.init(ownerView: scrollView, outermostView: view)
kbManager?.registerEditableField(nameTextField)
kbManager?.registerEditableField(addressTextField)
// more code ...
}
(可选步骤:)
2. 注销不需要包含或即将销毁的文本字段或文本视图
kbManager?.unregisterEditableField(nameTextField)
3. 从重写的 prepare 方法调用,如果您想要在切换到另一个屏幕时隐藏键盘(dismissDuringSegue == true)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// some code ...
kbManager?.preparingSegue()
// some other code ...
}
4. 从重写的 viewWillTransition 调用,如果您想要在设备旋转期间隐藏键盘(dismissDuringDeviceRotation == true)
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
// do whatever ...
kbManager?.viewWillTransition()
}
5. 如果您希望在自定义手势后隐藏键盘,请注册您自己的自定义手势识别器。您可以为此类手势识别器添加额外的动作目标,但不要将它们添加到任何视图中或分配委托给它们,键盘管理器会处理这些操作。
.
.
tripleTapRecognizer.addTarget(self, action: #selector(handle3Taps(_:)))
kbManager?.registerCustomGestureRecognizer(tripleTapRecognizer)
.
.
6. 根据需要更改其他用户选项
option default meaning for true
______________________________________________________________________
dismissOnTapGestures true if taps are detected outside
of keyboard and not in text
fields, keyboard will dismiss
dismissOnPanGestures true if pans are detected outside
of keyboard and not in text
fields, keyboard will dismiss
false (set to false by default for
HKUIKeyboardManagerScrollable to
allow scrolling)
dismissOnPinchGestures true if pinches are detected outside
of keyboard and not in text
fields, keyboard will dismiss
dismissOnRotationGestures true if rotation gestures are detected
outside of keyboard and not in
text fields, keyboard will dismiss
dismissDuringSegue true dismiss the keyboard when a segue
is being prepared, only set to
false if the view does not rotate.
Subject to automatically setting
to true if the view rotates and
this is set to false.
See development notes.
dismissDuringDeviceRotation true dismiss the keyboard when the
device is rotated, only set to
false if the view does not segue
to another screen.
Subject to automatically setting
to true if a segue is prepared and
this is set to false.
See development notes.
keepActiveFieldInViews true (HKUIKeyboardManagerScrollable
only)
if the active text field is
obscured by the keyboard when it
first receive focus, it will be
scrolled to above the keyboard
but if doing so will cause the
top of the field to extend above
the top of the screen, it will
simply be scrolled down so that
its top is just under the top of
the screen with a small margin.
定制它
如果您想修改代码,请阅读单独文件中的 开发注意事项,以了解您应该注意的问题。
已知问题
目前没有已知问题。