Last active
February 18, 2016 09:31
-
-
Save fobos000/3cddebaae629642856d8 to your computer and use it in GitHub Desktop.
iOS helper class for discovering current keyboard frame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface KeyboardHelper : NSObject | |
@property (nonatomic, readonly) CGRect keyboardBounds; | |
+ (KeyboardHelper *)instance; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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