KeyboardManager

public class KeyboardManager

Push/pull views when the keyboard appears and disappears.

In your UIViewController, add a KeyboardManager property:

   var kb: KeyboardManager?

In viewWillAppear(_:), initialize it and use the according method to subscribe to the show and hide notifications:

   override func viewWillAppear(_ animated: Bool) {
       super.viewWillAppear(animated)

       kb = KeyboardManager(viewsToPushUp: [textField, button])
       kb?.pushViewsUpWhenKeyboardWillShow()
       kb?.pullViewsDownWhenKeyboardWillHide()
   }

Although not necessary anymore, if you want to stop listening to the notifications, you can call kb?.stopListeningToKeyboardNotifications() within viewWillDisappear(_:) for example.

  • Sets which object should trigger the notification (default to all with nil)

    Declaration

    Swift

    public var notifyFromObject: Any?
  • The arrays of views that will be moved

    Declaration

    Swift

    public var viewsToPushUp: [UIView] = []
  • Init a KeyboardManager

    Parameter

    Parameter viewsToPushUp: the arrays of views to move up/down

    Parameter

    Parameter notifyFromObject: which object should trigger the notification (default to all with nil)

    Declaration

    Swift

    public init(viewsToPushUp: [UIView], notifyFromObject: Any? = nil)

    Parameters

    viewsToPushUp

    the arrays of views to move up/down

    notifyFromObject

    which object should trigger the notification (default to all with nil)

  • Subscribe to the UIKeyboardWillShow notification and pushes the selected view up by the height of the keyboard

    Declaration

    Swift

    public func pushViewsUpWhenKeyboardWillShow()
  • Subscribe to the UIKeyboardWillHide notification and pulls the selected view down by the height of the keyboard

    Declaration

    Swift

    public func pullViewsDownWhenKeyboardWillHide()
  • Unsubscribe from the keyboard notifications

    Declaration

    Swift

    public func stopListeningToKeyboardNotifications()