How the library achieves this is by having it’s manager listening to the standard UIKeyboardWillShowNotification during app initialization, and then adjusting the whole window view when necessary.
It’s a shame iOS didn’t ship with a capability like that.
How to manually handle the keyboard
While IQKeyboardManager is great, sometimes you want to manage these keyboard events manually.
For example, if you have a UITextView for entering paragraph of text, and you want only the insets to change when keyboard is showing/hiding, then IQKeyboardManager isn’t configured for that.
Or for example, if you have a toolbar that you want to stick just above the keyboard, then IQKeyboardManager isn’t suited for that.
This is the code to manually handle, and with proper animations that synchronize with keyboard animation:
-(void)viewDidLoad{[superviewDidLoad];// Listen to the standard keyboard notifications[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];}-(void)dealloc{[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardDidShowNotificationobject:nil];[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];}-(void)keyboardWillShow:(NSNotification*)aNotification{[selfadjustForKeyboard:aNotificationshowing:YES];}-(void)keyboardWillHide:(NSNotification*)aNotification{[selfadjustForKeyboard:aNotificationshowing:NO];}-(void)adjustForKeyboard:(NSNotification*)aNotificationshowing:(BOOL)showing{NSDictionary*info=[aNotificationuserInfo];CGRectkeyboardFrame=[[infoobjectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];CGRectconvertedFrame=[self.viewconvertRect:keyboardFramefromView:self.view.window];CGSizekbSize=convertedFrame.size;// 1. Adjust for the UITextViewUIEdgeInsetsnewInsets;// If keyboard showing, add an insetif(showing){newInsets=UIEdgeInsetsMake(0.0,0.0,kbSize.height,0.0);}else{// Else keyboard hiding, rest the insetnewInsets=UIEdgeInsetsMake(0,0,0,0);}_textView.contentInset=newInsets;_textView.scrollIndicatorInsets=newInsets;// 2. Adjust toolbar with animation// Get keyboard animation infoNSTimeIntervalanimationDuration;UIViewAnimationCurveanimationCurve;[[infoobjectForKey:UIKeyboardAnimationCurveUserInfoKey]getValue:&animationCurve];[[infoobjectForKey:UIKeyboardAnimationDurationUserInfoKey]getValue:&animationDuration];// A simple convertion of UIViewAnimationCurve to UIViewAnimationOptionsUIViewAnimationOptionsanimationOption=animationCurve<<16;if(animationDuration==0)animationDuration=0.1;// Start animation[self.viewlayoutIfNeeded];[UIViewanimateWithDuration:animationDurationdelay:0options:animationOptionanimations:^{// Adjust for toolbarif(showing)_toolbarBottomSpace.constant=kbSize.height;else_toolbarBottomSpace.constant=0;[self.viewlayoutIfNeeded];}completion:nil];}
In viewDidLoad, it is where we add the view controller as the observer to the keyboard events. Subsequently in dealloc, remove the observer.
The gist is in adjustForKeyboard:showing:.
aNotification contains a dictionary about the keyboard size and animation details.
A little trick is needed to convert UIViewAnimationCurve to UIViewAnimationOptions, which is needed for the new animation code.
For the toolbar, we change _toolbarBottomSpace, which is a vertical spacing constraint for the toolbar view to the bottom layout guide. You need to create this constraint on your storyboard (with autolayout).