Simple iOS Keyboard frame tracker for custom interactive Keyboard dismissal
- 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
withinputAccessoryView
To run the example project, clone the repo, and run the .xcworkspace.
AMKeyboardFrameTracker is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'AMKeyboardFrameTracker'
- Requires iOS 9.0+
let height = 60 // this should be your input view height
let keyboardFrameTrackerView = AMKeyboardFrameTrackerView.init(height: height)
inputTextView.inputAccessoryView = keyboardFrameTrackerView
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
}
keyboardFrameTrackerView.onKeyboardFrameDidChange = { [weak self] frame in
guard let self = self else {return}
print("Keyboard frame: ", frame)
}
keyboardFrameTrackerView.delegate = self
extension ExampleViewController: AMKeyboardFrameTrackerDelegate {
func keyboardFrameDidChange(with frame: CGRect) {
print("Keyboard frame: ", frame)
}
}
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()
}
}
AMKeyboardFrameTracker is available under the MIT license. See the LICENSE file for more info.