RxKeyboard

repository·master·Indexed 23 days ago

https://github.com/rxswiftcommunity/rxkeyboard

A reactive wrapper for observing keyboard frame changes in iOS. It provides RxSwift Drivers—including frame, visibleHeight, and willShowVisibleHeight—to simplify adjusting UI elements like ScrollViews and Toolbars in response to keyboard updates. Requires Swift 5.1+, iOS 9+, RxSwift >= 6.0, and RxCocoa >= 6.0.

Tokens
963
Snippets
5
Records
7
Agent score
32%

What's inside RxKeyboard

  1. Install RxKeyboard via CocoaPods or Carthage

    master

    You can install RxKeyboard using either CocoaPods or Carthage.

    CocoaPods Add the following to your Podfile:

    pod 'RxKeyboard'

    Carthage Use the binary installation method:

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

    Note: Carthage only supports binary installation for specific older versions (0.7.0 through 0.9.2) with corresponding Xcode and Swift versions.

  2. Build the RxKeyboardExample project

    master

    To build and run the provided messenger application example, follow these steps:

    1. Install the necessary CocoaPods dependencies by running pod install in the project directory.
    2. Open the RxKeyboardExample.xcworkspace file in Xcode.
    3. Build and run the project using the shortcut ⌘ + R.
    $ pod install
  3. Observe keyboard frame changes with RxKeyboard

    master

    RxKeyboard provides a singleton instance RxKeyboard.instance to access reactive observers for the keyboard. You can subscribe to the frame Driver to observe changes to the keyboard's CGRect frame.

    Requirements:

    • Swift 5.1+
    • iOS 9+
    • RxSwift >= 6.0
    • RxCocoa >= 6.0
    RxKeyboard.instance.frame
      .drive(onNext: { frame in
        print(frame)
      })
      .disposed(by: disposeBag)
  4. Adjust UIScrollView contentInset to fit keyboard height

    master

    To prevent the keyboard from covering content in a UIScrollView, use the visibleHeight Driver to update the scroll view's contentInset.bottom.

    RxKeyboard.instance.visibleHeight
      .drive(onNext: { [scrollView] keyboardVisibleHeight in
        scrollView.contentInset.bottom = keyboardVisibleHeight
      })
      .disposed(by: disposeBag)
  5. Move a UIToolbar with the keyboard

    master

    You can make a UIToolbar follow the keyboard height, which is particularly useful for UIScrollViewKeyboardDismissMode.interactive.

    Using Auto Layout Capture the toolbar's bottom constraint and set its constant to the negative of the keyboard visible height. It is recommended to wrap this in an animation block using setNeedsLayout() and layoutIfNeeded().

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

    Without Auto Layout Manually update the toolbar's frame origin.

    RxKeyboard.instance.visibleHeight
      .drive(onNext: { [toolbar, view] keyboardVisibleHeight in
        toolbar.frame.origin.y = view.frame.height - toolbar.frame.height - keyboardVisibleHeight
      })
      .disposed(by: disposeBag)
  6. Adjust UIScrollView contentOffset to fit keyboard height

    master

    To adjust the contentOffset of a UIScrollView when the keyboard is about to appear, use the willShowVisibleHeight Driver.

    RxKeyboard.instance.willShowVisibleHeight
      .drive(onNext: { [scrollView] keyboardVisibleHeight in
        scrollView.contentOffset.y += keyboardVisibleHeight
      })
      .disposed(by: disposeBag)
  7. Use RxKeyboard Drivers to observe keyboard state

    master

    RxKeyboard exposes three primary Driver types to observe keyboard behavior:

    • frame: A Driver<CGRect> that emits the current keyboard frame.
    • visibleHeight: A Driver<CGFloat> that emits the visible height of the keyboard. It emits the actual height when the keyboard is visible, or 0 when it is not.
    • willShowVisibleHeight: A Driver<CGFloat> similar to visibleHeight, but it only emits values when the keyboard is about to show. This is specifically useful for adjusting UIScrollView content offsets before the keyboard appears.