[](https://travis-ci.org/Valery Fomenko/VFKeyboardObserver)
A simple way to observe keyboard notifications and keyboard properties.
- Start / stop observing keyboard events. ==
// Create new observer, or use shared instance
[[VFKeyboardObserver sharedKeyboardObserver] start];
[[VFKeyboardObserver sharedKeyboardObserver] stop];
- Add / remove delegates ==
Adopt to VFKeyboardObserverDelegate
protocol and add an object as a delegate.
VFKeyboardObserver can have any number of delegates. Delegates are not retained.
@protocol VFKeyboardObserverDelegate <NSObject>
@optional
- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardWillShowWithProperties:(VFKeyboardProperties)keyboardProperties interfaceOrientationWillChange:(BOOL)interfaceOrientationWillChange;
- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardDidShowWithProperties:(VFKeyboardProperties)keyboardProperties;
- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardWillHideWithProperties:(VFKeyboardProperties)keyboardProperties;
- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardDidHideWithProperties:(VFKeyboardProperties)keyboardProperties;
@end
[[VFKeyboardObserver sharedKeyboardObserver] addDelegate:aDelegate];
[[VFKeyboardObserver sharedKeyboardObserver] removeDelegate:aDelegate];
- Update UI when keyboard appears / disappears ==
It is common to update UI when keyboard will appear / will disappear.
- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardWillShowWithProperties:(VFKeyboardProperties)keyboardProperties interfaceOrientationWillChange:(BOOL)interfaceOrientationWillChange {
[keyboardObserver animateWithKeyboardProperties:^{
// ...
}];
}
- (void)keyboardObserver:(VFKeyboardObserver *)keyboardObserver keyboardWillHideWithProperties:(VFKeyboardProperties)keyboardProperties {
[keyboardObserver animateWithKeyboardProperties:^{
// ...
}];
}
VFKeyboardObserver have convenient method animateWithKeyboardProperties:
that performs animations synched with keyboard animation.
- Rotation ==
If we animate our UI to adjust it to keyboard, for example, attach UITextField
to the top of the keyboard, there is one problem when interface orientation changes. Our UITextField
does not stay attached while the rotation of interface orientation is animated.
See this for details: http://smnh.me/synchronizing-rotation-animation-between-the-keyboard-and-the-attached-view/
To solve this problem, VFKeyboardObserver has interfaceOrientationWillChange
method.
Call it from UIViewController's viewWillTransitionToSize:withTransitionCoordinator:
.
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[[VFKeyboardObserver sharedKeyboardObserver] interfaceOrientationWillChange];
}
In this case, (only while interface orientation changes):
VFKeyboardObserverDelegate
will receive only two messages: keyboardWillShow and keyboardDidShow (hide methods will be ignored).interfaceOrientationWillChange
argument ofkeyboardWillShow
delegate method will be YES, so in case of custom UI adjusting animations in this method, you can do it without animationanimateWithKeyboardProperties:
methods automatically performanimations
andcompletion
blocks without animation (if you use it, no changes needed).
VFKeyboardObserver is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "VFKeyboardObserver"
Valery Fomenko, [email protected]
VFKeyboardObserver is available under the MIT license. See the LICENSE file for more info.