Swiftpack.co - Package - RxSwiftCommunity/RxKeyboard

RxKeyboard

Swift CocoaPods Build Status Carthage compatible

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: { [scrollView] 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: { [scrollView] keyboardVisibleHeight in
        scrollView.contentOffset.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: { [toolbar, view] keyboardVisibleHeight in
        toolbar.frame.origin.y = view.frame.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: { [toolbarBottomConstraint] 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

In development, RxKeyboard manages dependencies with Swift Package Manager. Use the command below in order to generate a Xcode project file. Note that .xcodeproj file changes are not tracked via git.

$ swift package generate-xcodeproj

Installation

  • Using CocoaPods:

    pod 'RxKeyboard'
    
  • Using Carthage:

    binary "https://raw.githubusercontent.com/RxSwiftCommunity/RxKeyboard/master/RxKeyboard.json"
    

    ⚠️ With Carthage, RxKeyboard only supports binary installation:

    • 0.9.2
      • Xcode 10.1 (10B61)
      • Swift 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1)
    • 0.9.0
      • Xcode 10 (10A255)
      • Swift 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1)
    • 0.8.2
      • Xcode 9.3 (9E145)
      • Swift 4.1.0 (swiftlang-902.0.48 clang-902.0.37.1)
    • 0.7.1
      • Xcode 9.1 (9B55)
      • Swift 4.0.2 (swiftlang-900.0.69.2 clang-900.0.38)
    • 0.7.0
      • 9.0.1 (9A1004)
      • Swift 4.0 (swiftlang-900.0.65.2 clang-900.0.37)

License

RxKeyboard is under MIT license.

Github

link
Stars: 1186

Dependencies

Used By

Total: 0

Releases

RxKeyboard 1.0.0 - 2019-05-27 21:30:24

Support Swift 5, RxSwift 5 and Xcode 10.2.

0.9.2 - 2019-01-12 00:05:17

Support Carthage binary for Swift 4.2.1

- 2018-10-12 19:56:08

0.9.0 - 2018-09-23 12:30:33

Support for Swift 4.2 & Xcode 10

- 2018-06-10 22:17:41

  • Support Xcode 9.4 and Swift 4.1.2

- 2018-03-30 11:11:00

  • Support Swift 4.1

- 2017-12-28 14:22:39

  • Fix a bug that willShowVisibleHeight emits a value when hiding a keyboard (#52, @cpwhidden)

- 2017-12-08 07:14:32

  • Add RxKeyboardType protocol (#50)
  • Add Xcode 9.2 support for Carthage

- 2017-11-01 09:18:19

  • Add Xcode 9.1 support for Carthage

- 2017-09-22 09:48:23

  • Add Swift 4 support ✨ (Thanks to @EvsenevDev)

- 2017-08-10 15:17:59

  • Fix Carthage building (#31, @brentleyjones)

- 2017-08-07 08:44:23

  • Ignore first event from willShowVisibleHeight (#30, @vbmania)

- 2017-07-31 05:04:59

  • Add isHidden property to easily observe keyboard visibility (#27, @noppefoxwolf)

- 2017-07-06 07:07:28

  • Fix Carthage warning when building by including in workspace (#26, @brentleyjones)

- 2017-05-09 13:48:24

  • Update RxSwift dependency to 3.4 (#20, @demchenkoalex)

- 2017-01-23 03:14:01

  • Reorganize the folder and project structure so that RxKeyboard target doesn't contain CocoaPods related resources (#18)

- 2017-01-18 11:48:25

  • Take only .changed state from pan gesture recognizer (#14, @devxoul)
  • Add willShowVisibleHeight property (#15, @devxoul)

- 2016-10-25 11:40:48

  • Update RxSwift dependency to >= 3.0 (#5, @devxoul)

- 2016-10-18 15:50:51

  • Add Carthage support (#2, @devxoul)
  • Use RxSwift 3.0.0-rc.1 (#3, @devxoul)

- 2016-10-18 15:50:23

Hello, world!