Latest 1.0
Homepage https://github.com/Altimor/KWKeyboardListener
License GPL-3
Platforms ios 5.0, requires ARC
Authors

Easy way to make a class react to keyboard events.

Register a new listener

Before:

- (void)registerMyClass {
   NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
   [defaultCenter addObserver:self selector:@selector(didShow:) name:UIKeyboardDidShowNotification object:nil];
   [defaultCenter addObserver:self selector:@selector(didHide:) name:UIKeyboardDidHideNotification object:nil];
   [defaultCenter addObserver:self selector:@selector(willShow:) name:UIKeyboardWillShowNotification object:nil];
   [defaultCenter addObserver:self selector:@selector(willHide:) name:UIKeyboardWillHideNotification object:nil];
   [defaultCenter addObserver:self selector:@selector(didChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];
   [defaultCenter addObserver:self selector:@selector(willChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

// No autocompletion here, you have to type the methods' name…
- (void)didShow:(NSNotification *)notification {
...
}

- (void)willShow:(NSNotification *)notification {
...
}

- (void)didHide:(NSNotification *)notification {
...
}

- (void)willHide:(NSNotification *)notification {
...
}

- (void)didChangeFrame:(NSNotification *)notification {
...
}

- (void)willChangeFrame:(NSNotification *)notification {
...
}

After:

- (void)registerMyClass {
  [[KWKeyboardListener sharedInstance] addKeyboardEventsListener:self withHandler:^(CGRect keyboardFrame, BOOL opening, BOOL closing) {
      // This block will get called when the keyboard *will* show/hide/change frame
  }];
}

You can also make your class conform to the KWKeyboardEventsListener protocol, call [[KWKeyboardListener sharedInstance] addKeyboardEventsListener:self] and implement the protocol’s methods you wish. The advantage of this method over NSNotificationCenter is that you do not have to register your class one time for each event you want to react to, and thanks to the protocol you’ll have auto-completion, so it is easy to react to a new event simply by begin typing - (void)keyboard and see the list of supported events, which are:

- (void)keyboardWillShowWithInfos:(NSDictionary *)infos;
- (void)keyboardWillHideWithInfos:(NSDictionary *)infos;
- (void)keyboardDidShowWithInfos:(NSDictionary *)infos;
- (void)keyboardDidHideWithInfos:(NSDictionary *)infos;
- (void)keyboardWillChangeFrameWithInfos:(NSDictionary *)infos;
- (void)keyboardDidChangeFrameWithInfos:(NSDictionary *)infos;

The infos are simply the userInfos you would normally receive from the regular NSNotification. You can get the informations relative to the event with the regular notificaton keys, as defined in UIKit:

UIKIT_EXTERN NSString *const UIKeyboardFrameBeginUserInfoKey        NS_AVAILABLE_IOS(3_2); // NSValue of CGRect
UIKIT_EXTERN NSString *const UIKeyboardFrameEndUserInfoKey          NS_AVAILABLE_IOS(3_2); // NSValue of CGRect
UIKIT_EXTERN NSString *const UIKeyboardAnimationDurationUserInfoKey NS_AVAILABLE_IOS(3_0); // NSNumber of double
UIKIT_EXTERN NSString *const UIKeyboardAnimationCurveUserInfoKey    NS_AVAILABLE_IOS(3_0); // NSNumber of NSUInteger 

Know the keyboard state

Moreover, the KWKeyboardListener singleton, which is instantiated at the class load, so you don’t have to do anything, allows you to get the current state of the keyboard from anywhere in the app, using its keyboardVisible boolean property.

Pod

pod 'KWKeyboardListener', '~>1.0'

Latest podspec

{
    "name": "KWKeyboardListener",
    "version": "1.0",
    "summary": "Easy way to make a class react to keyboard events.",
    "description": "tt   Make your classes listen to and react to Keyboard events in one line of code.ntttSupported events are:nttt* Will Shownttt* Did Shownttt* Will Change Framenttt* Did Change Framenttt* Will Hidenttt* Did HidentttntttJust use the class's add/removeListener methods and implement the events you wish to support.n",
    "homepage": "https://github.com/Altimor/KWKeyboardListener",
    "license": "GPL-3",
    "authors": {
        "Florent Crivello": "[email protected]"
    },
    "platforms": {
        "ios": "5.0"
    },
    "requires_arc": true,
    "source": {
        "git": "https://github.com/Altimor/KWKeyboardListener.git",
        "tag": "1.0"
    },
    "source_files": [
        "KeyboardListener/KWKeyboardListener",
        "KeyboardListener/KWKeyboardListener/*.{h,m}"
    ]
}

Pin It on Pinterest

Share This