KeyboardObserver

public final class KeyboardObserver

Mechanica

KeyboardObserver is an UIKit keyboard’s behavior observer (for iOS apps) without Notification Center. Declare what should happen on what event and register().

Example:

let keyboard = KeyboardObserver()
keyboard
 .on(event: .didShow) { (options) in
   print("didShow")
 }
 .on(event: .didHide) { (options) in
   print("didHide")
 }
 .register()

You must call register() for callbacks to be triggered.

Calling unregister() will stop callbacks from triggering (UIKit’s notifications won’t be observed anymore), but callbacks themselves won’t be dismissed; you can resume event callbacks by calling register() again.

To remove all event callbacks, call clear().

  • Mechanica

    A KeyboardObserver callback.

    Declaration

    Swift

    public typealias Callback = (Options) -> Void
  • Mechanica

    Struct containing all data that a keyboard has at the event of happening.

    See more

    Declaration

    Swift

    public struct Options
  • Mechanica

    Keyboard events that can happen. Translates directly to UIKeyboard notifications from UIKit.

    See more

    Declaration

    Swift

    public enum Event
  • Mechanica

    Defines KeyboardObserver behaviours.

    Warning

    Without calling register() none of the closures will be executed.

    Warning

    If more closures are declared on the same event, only the latter will be executed.

    Declaration

    Swift

    public final func on(event: Event, do callback: Callback?) -> Self

    Parameters

    event

    KeyboardObserver event on which callback should be executed.

    callback

    Closure of code which will be executed on KeyboardObserver.

    Return Value

    Self for chaining purpose.

  • Mechanica

    Initializes a new instance of KeyboardObserver.

    Declaration

    Swift

    public init()
  • Mechanica

    Registers for KeyboardObserver events and starts calling corresponding event handlers.

    Declaration

    Swift

    public final func register()
  • Mechanica

    Remove all event callbacks.

    Declaration

    Swift

    public final func clear()
  • Mechanica

    Unregister from KeyboardObserver events.

    Warning

    callbacks themselves won’t be dismissed.

    Declaration

    Swift

    public final func unregister()