Skip to content

Instantly share code, notes, and snippets.

@fobos000
Last active February 18, 2016 09:31
Show Gist options
  • Save fobos000/3cddebaae629642856d8 to your computer and use it in GitHub Desktop.
Save fobos000/3cddebaae629642856d8 to your computer and use it in GitHub Desktop.
iOS helper class for discovering current keyboard frame
#import <Foundation/Foundation.h>
@interface KeyboardHelper : NSObject
@property (nonatomic, readonly) CGRect keyboardBounds;
+ (KeyboardHelper *)instance;
@end
#import "KeyboardHelper.h"
@implementation KeyboardHelper
+ (KeyboardHelper *)instance {
static KeyboardHelper *_instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] init];
});
return _instance;
}
+ (void)load {
[[NSNotificationCenter defaultCenter] addObserver:[self instance]
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
// Register notification when the keyboard will be hide
[[NSNotificationCenter defaultCenter] addObserver:[self instance]
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
_keyboardBounds = [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
}
- (void)keyboardWillHide:(NSNotification *)notification {
_keyboardBounds = [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment