RxKeyboard 0.5.0

RxKeyboard 0.5.0

TestsTested โœ—
LangLanguage SwiftSwift
License MIT
ReleasedLast Release May 2017
SwiftSwift Version 3.0
SPMSupports SPM โœ—

Maintained by Suyeol Jeon.


Downloads

Total60,044
Week4,611
Month19,354

Installs

Apps636
Apps WeekApps This Week 225
Test Targets90
Test WeekTests This week 36
powered by Segment

GitHub

Stars470
Watchers42
Forks25
Issues2
Contributors3
Pull Requests2

Code

Files1
LOCLines of Code 101


 
Depends on:
RxSwift>= 3.4
RxCocoa>= 3.4
 

  • By
  • Suyeol Jeon

RxKeyboard

RxKeyboard provides a reactive way of observing keyboard frame changes. Forget about keyboard notifications. It also perfectly works with UIScrollViewKeyboardDismissMode.interactive.

rxkeyboard-message rxkeyboard-textview

Getting Started

RxKeyboard provides two Drivers.

/// An observable keyboard frame.
let frame: Driver<CGRect>

/// An observable visible height of keyboard. Emits keyboard height if the keyboard is visible
/// or `0` if the keyboard is not visible.
let visibleHeight: Driver<CGFloat>

/// Same with `visibleHeight` but only emits values when keyboard is about to show. This is
/// useful when adjusting scroll view content offset.
let willShowVisibleHeight: Driver<CGFloat>

Use RxKeyboard.instance to get singleton instance.

RxKeyboard.instance

Subscribe RxKeyboard.instance.frame to observe keyboard frame changes.

RxKeyboard.instance.frame
  .drive(onNext: { frame in
    print(frame)
  })
  .addDisposableTo(disposeBag)

Tips and Tricks

  • ๐Ÿ”— I want to adjust UIScrollViewโ€™s contentInset to fit keyboard height.

    RxKeyboard.instance.visibleHeight
      .drive(onNext: { keyboardVisibleHeight in
        scrollView.contentInset.bottom = keyboardVisibleHeight
      })
      .addDisposableTo(disposeBag)
  • ๐Ÿ”— I want to adjust UIScrollViewโ€™s contentOffset to fit keyboard height.

    RxKeyboard.instance.willShowVisibleHeight
      .drive(onNext: { keyboardVisibleHeight in
        scrollView.contentInset.offset.y += keyboardVisibleHeight
      })
      .addDisposableTo(disposeBag)
  • ๐Ÿ”— I want to make UIToolbar move along with the keyboard in an interactive dismiss mode. (Just like the wonderful GIF above!)

    If youโ€™re not using Auto Layout:

    RxKeyboard.instance.visibleHeight
      .drive(onNext: { keyboardVisibleHeight in
        toolbar.frame.origin.y = self.view.height - toolbar.frame.height - keyboardVisibleHeight
      })
      .addDisposableTo(disposeBag)

    If youโ€™re using Auto Layout, you have to capture the toolbarโ€™s bottom constraint and set constant to keyboard visible height.

    RxKeyboard.instance.visibleHeight
      .drive(onNext: { keyboardVisibleHeight in
        toolbarBottomConstraint.constant = -1 * keyboardVisibleHeight
      })
      .addDisposableTo(disposeBag)

    Note: In real world, you should use setNeedsLayout() and layoutIfNeeded() with animation block. See the demo project for example.

  • Anything else? Please open an issue or make a Pull Request.

Dependencies

Requirements

  • Swift 3
  • iOS 8+

Installation

  • Using CocoaPods:

    pod 'RxKeyboard', '~> 0.4'
  • Using Carthage:

    github "devxoul/RxKeyboard" ~> 0.4
    

License

RxKeyboard is under MIT license.