UnderKeyboard

Introduction

UnderKeyboard is a powerful framework for handling the keyboard interaction within your iOS application. It provides convenient functionality for managing the keyboard appearance and handling keyboard events with ease.

Features

  • Automatically adjust the view’s position when the keyboard appears or disappears
  • Customize the keyboard animation and duration
  • Handle keyboard events such as the keyboard will show/hide
  • Convenient keyboard height retrieval
  • Easily enable/disable automatic keyboard handling on specific views
  • Support for both portrait and landscape orientations

Requirements

  • iOS 10.0+
  • Swift 5.0+

Installation

To install UnderKeyboard using Carthage, add the following line to your Cartfile:

github "underxsystem/UnderKeyboard"

For CocoaPods, add the following line to your Podfile:

pod 'UnderKeyboard'

Usage

To start using UnderKeyboard, follow these steps:

Importing the framework

import UnderKeyboard

Enabling automatic keyboard handling

To enable automatic keyboard handling on your desired view, simply call the following method:

yourView.startUnderKeyboardHandling()

If you wish to disable automatic keyboard handling, use:

yourView.stopUnderKeyboardHandling()

Customizing keyboard animation

You can customize the animation by implementing the optional protocol UnderKeyboardHandlingDelegate and adopting it in your desired class.


class YourViewController: UIViewController, UnderKeyboardHandlingDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        yourView.underKeyboardHandlingDelegate = self
    }

    // MARK: - UnderKeyboardHandlingDelegate

    func keyboardAnimationOptions(forShowing showing: Bool) -> UIView.AnimationOptions {
        if showing {
            // Return the desired animation options when the keyboard is about to show
            return .curveEaseInOut
        } else {
            // Return the desired animation options when the keyboard is about to hide
            return .curveLinear
        }
    }

    func keyboardAnimationDuration(forShowing showing: Bool) -> TimeInterval {
        if showing {
            // Return the desired animation duration when the keyboard is about to show
            return 0.3
        } else {
            // Return the desired animation duration when the keyboard is about to hide
            return 0.2
        }
    }
}

Further Customizations

UnderKeyboard offers some additional customization options:

Retrieving keyboard height

You can obtain the currently visible keyboard height by calling:

let keyboardHeight = UnderKeyboard.shared.keyboardHeight

Observing keyboard status

UnderKeyboard provides convenient keyboard status observation by notification:


NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UnderKeyboardNotification.keyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UnderKeyboardNotification.keyboardWillHide, object: nil)

@objc func keyboardWillShow() {
    // Handle keyboard will show event
}

@objc func keyboardWillHide() {
    // Handle keyboard will hide event
}

Contributions

Contributions to UnderKeyboard are welcomed! If you encounter any issues or have suggestions for improvements, please feel free to open a GitHub issue.

License

UnderKeyboard is released under the MIT License. See LICENSE for details.

Conclusion

UnderKeyboard simplifies keyboard management in your iOS app, providing a seamless user experience. With its powerful features and straightforward integration, you can handle keyboard events efficiently and focus on building great apps.