Skip to content

Simple iOS Keyboard frame tracker for custom interactive Keyboard dismissal

License

Notifications You must be signed in to change notification settings

iAmrMohamed/AMKeyboardFrameTracker

Repository files navigation

AMKeyboardFrameTracker

Simple iOS Keyboard frame tracker for custom interactive Keyboard dismissal

AMKeyboardFrameTracker

Features

  • Very simple and easy to use API
  • Provides both delegates and closure callbacks
  • Can be used with any type of input views (UITextField, UITextView)
  • Allows for interactive Keyboard dismissal in a UITabBarController with inputAccessoryView

Example

To run the example project, clone the repo, and run the .xcworkspace.

Installation

AMKeyboardFrameTracker is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'AMKeyboardFrameTracker'

Requirements

  • Requires iOS 9.0+

Usage

let height = 60 // this should be your input view height
let keyboardFrameTrackerView = AMKeyboardFrameTrackerView.init(height: height)
inputTextView.inputAccessoryView = keyboardFrameTrackerView

Note

if your inputView height changes dynamically depending on the content inside it, then you will need keep the inputAccessoryView height in sync with your inputView height, to do that you need to override the viewDidLayoutSubviews in your ViewController and use the code below

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    self.keyboardFrameTrackerView.setHeight(self.inputContainerView.frame.height)
}

or you can use layout constraints. You can't constrain directly because an input accessory view does not share a view hierarchy with the rest of the window, but you can update the constant in viewDidLayoutSubviews.

var keyboardFrameTrackerViewHeightConstraint: NSLayoutConstraint!
override func viewDidLoad() {
    super.viewDidLoad()
    self.keyboardFrameTrackerView.delegate = self
    self.inputTextView.inputAccessoryView = self.keyboardFrameTrackerView
    self.keyboardFrameTrackerView.translatesAutoresizingMaskIntoConstraints = false
    self.keyboardFrameTrackerViewHeightConstraint = self.keyboardFrameTrackerView.heightAnchor.constraint(equalToConstant: 0)
    self.keyboardFrameTrackerViewHeightConstraint.isActive = true
}

override func viewDidLayoutSubviews() {
     super.viewDidLayoutSubviews()
     self.keyboardFrameTrackerViewHeightConstraint.constant = self.inputTextView.frame.height
}

Closure Callbacks

keyboardFrameTrackerView.onKeyboardFrameDidChange = { [weak self] frame in
    guard let self = self else {return}
    print("Keyboard frame: ", frame)
}

Delegate Callbacks

keyboardFrameTrackerView.delegate = self
extension ExampleViewController: AMKeyboardFrameTrackerDelegate {
    func keyboardFrameDidChange(with frame: CGRect) {
        print("Keyboard frame: ", frame)
    }
}

UITabBarController Support

First you need to add your inputView to you ViewController view as a normal subview and then set all your constraints and layout, you will need to have a bottomConstraints from your inputView to ViewController view and use the code below

extension ExampleViewController: AMKeyboardFrameTrackerDelegate {
    func keyboardFrameDidChange(with frame: CGRect) {
        let tabBarHeight = self.tabBarController?.tabBar.frame.height ?? 0.0
        let bottomSpacing = self.view.frame.height - frame.origin.y - tabBarHeight - self.keyboardFrameTrackerView.frame.height

        self.inputViewBottomConstraint.constant = bottomSpacing > 0 ? bottomSpacing : 0
        self.view.layoutIfNeeded()
    }
}

Author

@iAmrMohamed

License

AMKeyboardFrameTracker is available under the MIT license. See the LICENSE file for more info.

About

Simple iOS Keyboard frame tracker for custom interactive Keyboard dismissal

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published