Skip to content

Commit 23321ce

Browse files
committed
Update documentations
1 parent 767ec14 commit 23321ce

File tree

2 files changed

+62
-13
lines changed

2 files changed

+62
-13
lines changed

README.md

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,30 @@
44
[![CocoaPods](http://img.shields.io/cocoapods/v/RxKeyboard.svg)](https://cocoapods.org/pods/RxKeyboard)
55
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
66

7-
RxKeyboard provides a reactive way of keyboard handling. Forget about `UIKeyboardWillShow` and `UIKeyboardWillHide`. It also perfectly works with `UIScrollViewKeyboardDismissMode.interactive`.
7+
RxKeyboard provides a reactive way of observing keyboard frame changes. Forget about keyboard notifications. It also perfectly works with `UIScrollViewKeyboardDismissMode.interactive`.
88

99
![rxkeyboard](https://cloud.githubusercontent.com/assets/931655/19223656/14bd915c-8eb0-11e6-93ea-7618fc9c5d81.gif)
1010

11-
## At a Glance
11+
## Getting Started
1212

13-
Observing keyboard frame:
13+
RxKeyboard provides two [`Driver`](https://github.com/ReactiveX/RxSwift/blob/master/Documentation/Units.md#driver-unit)s.
14+
15+
```swift
16+
/// An observable keyboard frame.
17+
let frame: Driver<CGRect>
18+
19+
/// An observable visible height of keyboard. Emits keyboard height if the keyboard is visible
20+
/// or `0` if the keyboard is not visible.
21+
let visibleHeight: Driver<CGFloat>
22+
```
23+
24+
Use `RxKeyboard.instance` to get singleton instance.
25+
26+
```swift
27+
RxKeyboard.instance
28+
```
29+
30+
Subscribe `RxKeyboard.instance.frame` to observe keyboard frame changes.
1431

1532
```swift
1633
RxKeyboard.instance.frame
@@ -20,17 +37,43 @@ RxKeyboard.instance.frame
2037
.addDisposableTo(disposeBag)
2138
```
2239

23-
Observing keyboard visible height:
40+
## Tips and Tricks
2441

25-
```swift
26-
RxKeyboard.instance.visibleHeight
27-
.drive(onNext: { keyboardVisibleHeight in
28-
toolbarBottomConstraint.constant = -1 * keyboardVisibleHeight
29-
})
30-
.addDisposableTo(disposeBag)
31-
```
32-
33-
> **Note**: In real world, you should use `setNeedsLayout` and `layoutIfNeeded`. See the [demo project](https://github.com/devxoul/RxKeyboard/blob/master/Demo/Sources/ViewControllers/ViewController.swift#L62-L70) for example.
42+
- <a name="tip-content-inset" href="#tip-content-inset">🔗</a> **I want to adjust `UIScrollView`'s `contentInset` to fit keyboard height.**
43+
44+
```swift
45+
RxKeyboard.instance.visibleHeight
46+
.drive(onNext: { keyboardVisibleHeight in
47+
scrollView.contentInset.bottom = keyboardVisibleHeight
48+
})
49+
.addDisposableTo(disposeBag)
50+
```
51+
52+
- <a name="tip-toolbar" href="#tip-toolbar">🔗</a> **I want to make `UIToolbar` move along with the keyboard in an interactive dismiss mode. (Just like the wonderful GIF above!)**
53+
54+
If you're not using Auto Layout:
55+
56+
```swift
57+
RxKeyboard.instance.visibleHeight
58+
.drive(onNext: { keyboardVisibleHeight in
59+
toolbar.frame.origin.y = self.view.height - toolbar.frame.height - keyboardVisibleHeight
60+
})
61+
.addDisposableTo(disposeBag)
62+
```
63+
64+
If you're using Auto Layout, you have to capture the toolbar's bottom constraint and set `constant` to keyboard visible height.
65+
66+
```swift
67+
RxKeyboard.instance.visibleHeight
68+
.drive(onNext: { keyboardVisibleHeight in
69+
toolbarBottomConstraint.constant = -1 * keyboardVisibleHeight
70+
})
71+
.addDisposableTo(disposeBag)
72+
```
73+
74+
> **Note**: In real world, you should use `setNeedsLayout()` and `layoutIfNeeded()` with animation block. See the [demo project](https://github.com/devxoul/RxKeyboard/blob/master/Demo/Sources/ViewControllers/ViewController.swift#L62-L70) for example.
75+
76+
- Anything else? Please open an issue or make a Pull Request.
3477

3578
## Dependencies
3679

Sources/RxKeyboard.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@ import UIKit
1111
import RxCocoa
1212
import RxSwift
1313

14+
/// RxKeyboard provides a reactive way of observing keyboard frame changes.
1415
public class RxKeyboard: NSObject {
1516

1617
// MARK: Public
1718

19+
/// Get a singleton instance.
1820
public static let instance = RxKeyboard()
1921

22+
/// An observable keyboard frame.
2023
public let frame: Driver<CGRect>
24+
25+
/// An observable visible height of keyboard. Emits keyboard height if the keyboard is visible
26+
/// or `0` if the keyboard is not visible.
2127
public let visibleHeight: Driver<CGFloat>
2228

2329

0 commit comments

Comments
 (0)