TNKeyboardControlWindow is a small utility that helps you integrate keyboard pan-to-dismiss functionality.
Download the folder 'TNKeyboardControlWindow' and add the files to your project.
Add the following line to your Podfile
pod 'TNKeyboardControlWindow'
In order to handle the keyboard panning accross the whole app, you need to handle it on the UIWindow level.
TNKeyboardControlWindow does that for you.
You need to #import <TNKeyboardControlWindow.h>
in your AppDelegate, and add the following function to the AppDelegate:
-(UIWindow *)window
{
return [TNKeyboardControlWindow window];
}
var window: UIWindow? {
get {
return TNKeyboardControlWindow.window as UIWindow?
} set {}
}
This will automatically handle dismissing the keyboard across the whole app by dragging!
In case you have your own custom UIWindow class and you still need to implement this, you can simply subclass TNKeyboardControlWindow instead of UIWindow.
In most of the View Contorllers in your app, you need to update the UI Layout to accomodate to the keyboard displaying on the screen.
In order to do that, implement <TNKeyboardListenerProtocol>
in your View Controller and add the method
-(void)keyboardDidChangeFrame:(CGRect)frame {
}
func keyboardDidChangeFrame(_ frame: CGRect) {
bottomConstraint.constant = 8 + UIScreen.main.bounds.size.height - frame.origin.y
}
You would also need to add your View Controller as a listener to the window by calling
[[TNKeyboardControlWindow window] addKeyboardFrameListener:self];
TNKeyboardControlWindow.window.addKeyboardFrameListener(self)
Ideally inside your
viewDidAppear
.
You also need to remove your View Controller when it's no longer displayed by calling
[[TNKeyboardControlWindow window] removeKeyboardFrameListener:self];
TNKeyboardControlWindow.window.removeKeyboardFrameListener(self)
The listener protocol has 4 optional methods that can be implemented in order to avoid having to use NSNotificationCenter to monitor keyboard activity
-(void)keyboardWillStartShowing;
-(void)keyboardDidFinishShowing;
-(void)keyboardWillStartHiding;
-(void)keyboardDidFinishHiding;
public func keyboardWillStartShowing()
public func keyboardDidFinishShowing()
public func keyboardWillStartHiding()
public func keyboardDidFinishHiding()
And that's it!
A sample project is included with different ViewControllers and different UI components that update with the keyboard.
Feel free to use 'TNKeyboardControlWindow' in any way you like. An attribution is not required, but is highly appreciated.