KeyboardNotification
KeyboardNotification 协议有助于获取所有键盘事件,如 显示/隐藏/框架更改,以及键盘的 框架 和 大小。
安装
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它
$ gem install cocoapods
要在使用 CocoaPods 的 Xcode 项目中集成 KeyboardNotification,在您的 Podfile
中指定它
target '<Your Target Name>' do
pod 'KeyboardNotification'
end
然后,运行以下命令
$ pod install
如何使用
使用起来非常简单,只需将 KeyboardNotification 协议应用于您的 Controller 类或任何类。为期望的 Notification.Keyboard 类型注册您的类。
class ViewController: UIViewController, KeyboardNotification {
...
override func viewDidLoad(_ animated: Bool) {
super.viewDidLoad(animated)
// Register Keboard notification with inline closure
registerKeyboardNotification(.willShow) { note _ in
// Get notifcation when Keyboard will appears
}
// register Keyboard notification with inline function
registerKeyboardNotification(.willHide, handler: willHideKeyboard)
}
func willHideKeyboard(_ note: Notification) {
// Write your code while hiding keyboard
....
// Access keyboard frame
let frame = note.keyboardFrame
}
}
更多详情请查阅示例项目。