Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.

Commit 8133ee7

Browse files
committed
Added usage instructions
1 parent 12ce049 commit 8133ee7

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

README.md

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,78 @@ let package = Package(
4141
**NOTE:** This doesn't currently work as SPM doesn't support iOS, but once it will we will already be supporting it! :)
4242

4343
## 🔧 Setup
44-
> **TODO:** Add instructions
44+
Implement `KeyboardNotificationDelegate` in your UIViewController.
45+
46+
```swift
47+
class ViewController: UIViewController, KeyboardNotificationDelegate
48+
```
49+
50+
Add a `KeyboardHelper` private variable and initialize it, setting the delegate.
51+
52+
```swift
53+
private var keyboardHelper : KeyboardHelper?
54+
...
55+
self.keyboardHelper = KeyboardHelper(delegate: self)
56+
```
57+
Implement the two methods in the `KeyboardNotificationDelegate`:
58+
59+
```swift
60+
public func keyboardWillAppear(info: KeyboardHelper.KeyboardAppearanceInfo)
61+
public func keyboardWillDisappear(info: KeyboardHelper.KeyboardAppearanceInfo)
62+
```
63+
64+
Both methods take as argument a `KeyboardAppearanceInfo` object, which is basically a wrapper over the `userInfo` dictionary of the `UIKeyboardWillShowNotification` and `UIKeyboardWillHideNotification` notifications.
65+
66+
One example of implementation for the two delegate methods is:
67+
68+
```swift
69+
func keyboardWillAppear(info: KeyboardAppearanceInfo) {
70+
UIView.animateWithDuration(NSTimeInterval(info.animationDuration),
71+
delay: 0,
72+
options: info.animationOptions,
73+
animations: {
74+
let insets = UIEdgeInsetsMake(0, 0, info.endFrame.size.height, 0)
75+
self.scrollView.contentInset = insets
76+
self.scrollView.scrollIndicatorInsets = insets
77+
},
78+
completion:nil)
79+
}
80+
81+
func keyboardWillDisappear(info: KeyboardAppearanceInfo) {
82+
UIView.animateWithDuration(NSTimeInterval(info.animationDuration),
83+
delay: 0,
84+
options: info.animationOptions,
85+
animations: {
86+
let insets = UIEdgeInsetsZero
87+
self.scrollView.contentInset = insets
88+
self.scrollView.scrollIndicatorInsets = insets
89+
},
90+
completion:nil)
91+
}
92+
```
93+
94+
The `KeyboardAppearanceInfo` object has the following properties:
95+
96+
* `beginFrame`: a `CGRect` corresponding to the value for `UIKeyboardFrameBeginUserInfoKey`
97+
* `endFrame `: a `CGRect` corresponding to the value for `UIKeyboardFrameEndUserInfoKey`
98+
* `belongsToCurrentApp `: a `Bool` corresponding to the value for `UIKeyboardIsLocalUserInfoKey`
99+
* `animationDuration `: a `Double` corresponding to the value for `UIKeyboardAnimationDurationUserInfoKey`
100+
* `animationCurve `: a `UIViewAnimationCurve` corresponding to the value for `UIKeyboardAnimationCurveUserInfoKey`
101+
* `animationOptions `: a `UIViewAnimationOptions ` from the value of `UIKeyboardAnimationCurveUserInfoKey`
102+
103+
`KeyboardAppearanceInfo` also has the convenience method `animateAlong:completion:`, which can be used like this:
104+
105+
```swift
106+
func keyboardWillAppear(info: KeyboardAppearanceInfo) {
107+
info.animateAlong({ () -> Void in
108+
let insets = UIEdgeInsetsMake(0, 0, info.endFrame.size.height, 0)
109+
self.scrollView.contentInset = insets
110+
self.scrollView.scrollIndicatorInsets = insets
111+
}) { finished in }
112+
113+
```
114+
to get the same effect as the initial `keyboardWillAppear:` implementation example above.
115+
45116

46117

47118
## 👥 Credits

0 commit comments

Comments
 (0)