RxKeyboard 0.7.0

RxKeyboard 0.7.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Sep 2017
SwiftSwift Version 4.0
SPMSupports SPM

Maintained by Suyeol Jeon.


Downloads

Total177,723
Week9,531
Month42,436

Installs

Apps1,232
Apps WeekApps This Week 352
Pod Tries1
Test Targets201
Test WeekTests This week 51
powered by Segment

GitHub

Stars571
Watchers39
Forks36
Issues3
Contributors6
Pull Requests2

Code

Files1
LOCLines of Code 106


 
Depends on:
RxSwift>= 4.0.0-beta.0
RxCocoa>= 4.0.0-beta.0
 

  • By
  • Suyeol Jeon

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)
  })
  .disposed(by: 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
      })
      .disposed(by: disposeBag)
  • 🔗 I want to adjust UIScrollView's contentOffset to fit keyboard height.

    RxKeyboard.instance.willShowVisibleHeight
      .drive(onNext: { keyboardVisibleHeight in
        scrollView.contentInset.offset.y += keyboardVisibleHeight
      })
      .disposed(by: 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
      })
      .disposed(by: 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
      })
      .disposed(by: disposeBag)

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

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

Dependencies

Requirements

  • Swift 4
  • iOS 8+

Contributing

$ swift package generate-xcodeproj

Installation

  • Using CocoaPods:

    pod 'RxKeyboard'
  • Using Carthage:

    github "RxSwiftCommunity/RxKeyboard"
    

License

RxKeyboard is under MIT license.