Skip to content

Instantly share code, notes, and snippets.

@leberwurstsaft
Created October 6, 2012 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leberwurstsaft/3845949 to your computer and use it in GitHub Desktop.
Save leberwurstsaft/3845949 to your computer and use it in GitHub Desktop.
OLGhostAlertView Issue #3
#import <Foundation/Foundation.h>
@interface LWSKeyboardListener : NSObject
@property (nonatomic, readonly) BOOL keyboardShowed;
@property (nonatomic, readonly) CGRect keyboardRect;
@property (nonatomic, readonly) CGFloat keyboardHeight;
+ (LWSKeyboardListener *)defaultListener;
@end
#import "LWSKeyboardListener.h"
@implementation LWSKeyboardListener
+ (LWSKeyboardListener *)defaultListener {
static dispatch_once_t once;
static LWSKeyboardListener *sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (CGFloat)keyboardHeight {
return MIN(_keyboardRect.size.width, _keyboardRect.size.height);
}
#pragma mark - Life cycle
- (id)init {
self = [super init];
if (self) {
[self registerNotifications];
}
return self;
}
- (void)dealloc {
[self unregisterNotifications];
}
#pragma mark - Register Notifications
- (void)registerNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)unregisterNotifications {
[[NSNotificationCenter defaultCenter] removeObserver: self];
}
#pragma mark - Handle notifications
- (void)keyboardDidShow:(NSNotification *)notif {
_keyboardShowed = YES;
NSValue *value = [[notif userInfo] objectForKey: UIKeyboardFrameEndUserInfoKey];
_keyboardRect = [value CGRectValue];
}
- (void)keyboardWillHide:(NSNotification *)notif {
_keyboardShowed = NO;
_keyboardRect = CGRectZero;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment