https://github.com/daltron/keyboardspy
The Easiest Way to Observe Keyboard Notifications in iOS
https://github.com/daltron/keyboardspy
ios ios-keyboard ios-keyboardmanager swift swift3
Last synced: about 1 month ago
JSON representation
The Easiest Way to Observe Keyboard Notifications in iOS
- Host: GitHub
- URL: https://github.com/daltron/keyboardspy
- Owner: Daltron
- License: mit
- Created: 2017-02-10T03:26:46.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-09-26T13:52:16.000Z (over 7 years ago)
- Last Synced: 2025-04-13T14:07:44.266Z (about 1 month ago)
- Topics: ios, ios-keyboard, ios-keyboardmanager, swift, swift3
- Language: Swift
- Size: 1.73 MB
- Stars: 10
- Watchers: 3
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[](http://cocoapods.org/pods/KeyboardSpy)
![]()
[](http://cocoapods.org/pods/KeyboardSpy)
[](http://cocoapods.org/pods/KeyboardSpy)### Written in Swift 4
KeyboardSpy is a super lightweight and easy to use wrapper that makes observing keyboard notifications in iOS a breeze.
## Requirements
- iOS 8.0+
- xCode 9.0+## Installation
### CocoaPods
To integrate KeyboardSpy into your xCode project using CocoaPods, specify it in your `Podfile`:
```ruby
pod 'KeyboardSpy'
```Then, run the following command:
```bash
$ pod install
```## Usage
KeyboardSpy uses a protocol based approach to observe keyboard notifications:
```swift
public protocol KeyboardSpyAgent {
var keyboardEventsToSpyOn: [KeyboardSpyEvent] { get }
func keyboardSpyEventProcessed(event:KeyboardSpyEvent, keyboardInfo: KeyboardSpyInfo)
}
```To add a spy, simply:
```swift
KeyboardSpy.spy(on: self)
```To remove a spy, simply:
```swift
KeyboardSpy.unspy(on: self)
```There are six different events you can spy on:
```swift
public enum KeyboardSpyEvent {
case willShow
case didShow
case willHide
case didHide
case willChangeFrame
case didChangeFrame
}
```You will get the following object for each event you spy on:
```swift
public class KeyboardSpyInfo: NSObject {
public private(set) var beginFrame: CGRect!
public private(set) var endFrame: CGRect!
public private(set) var animationCurve: UIViewAnimationCurve!
public private(set) var animationDuration: Double!
public private(set) var isLocal: Bool!
public var keyboardHeight: CGFloat
}
```### Example:
```swift
import KeyboardSpyclass KeyboardSpyViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
KeyboardSpy.spy(on: keyboardSpyView) // This can be placed anywhere
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
KeyboardSpy.unspy(on: keyboardSpyView) // This can be placed anywhere
}
}extension KeyboardSpyViewController: KeyboardSpyAgent {
internal var keyboardEventsToSpyOn: [KeyboardSpyEvent] {
return [.willShow, .willHide]
}
internal func keyboardSpyEventProcessed(event: KeyboardSpyEvent, keyboardInfo: KeyboardSpyInfo) {
if event == .willShow {
// Do something like moving a view above the keyboard
} else if event == .willHide {
// Do something like moving a view back to its original position
}
}}
```## Author
Dalton Hinterscher, [email protected]
## License
KeyboardSpy is available under the MIT license. See the LICENSE file for more info.