Skip to content

Instantly share code, notes, and snippets.

@mbrock
Created August 18, 2015 15:55
Show Gist options
  • Save mbrock/8ebb6b723f3185ee3c26 to your computer and use it in GitHub Desktop.
Save mbrock/8ebb6b723f3185ee3c26 to your computer and use it in GitHub Desktop.
// Simplest possible wrapper around IHKeyboardAvoiding.
//
// I don't really know how to Xcode, but I got it to work by:
//
// 1. Checking out the IHKeyboardAvoiding repository.
// https://github.com/IdleHandsApps/IHKeyboardAvoiding
//
// 2. Dragging the Files IHKeyboardAvoiding.{h,m} into my project.
//
// 3. Ensuring that both .m files are in the "Compile Sources" phase of the target.
//
// Then, in JavaScript:
//
// var KeyboardAvoiding = require("react-native").NativeModules.KeyboardAvoiding
//
// ...
// componentDidMount: function() {
// /* If you're unlucky, try using a timeout? */
// KeyboardAvoiding.avoid(React.findNodeHandle(this.refs.view))
// },
// componentWillUnmount: function() {
// KeyboardAvoiding.stopAvoiding()
// }
//
// I'm a lazy person and only bothered with the simplest API.
#import <Foundation/Foundation.h>
#import "IHKeyboardAvoiding.h"
#import "RCTBridgeModule.h"
#import "RCTBridge.h"
#import "RCTUIManager.h"
#import "RCTView.h"
#import "RCTSparseArray.h"
@interface RCTKeyboardAvoiding : NSObject <RCTBridgeModule>
@end
@implementation RCTKeyboardAvoiding
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE()
- (dispatch_queue_t)methodQueue {
return _bridge.uiManager.methodQueue;
}
RCT_EXPORT_METHOD(avoid:(nonnull NSNumber*) tag)
{
[_bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, RCTSparseArray *viewRegistry) {
RCTView *view = tag == nil ? nil : viewRegistry[tag];
RCTLogInfo(@"Avoiding %@ (%@)", tag, view);
[IHKeyboardAvoiding setKeyboardAvoidingMode: KeyboardAvoidingModeMaximum];
if (view)
[IHKeyboardAvoiding setAvoidingView: view];
}];
}
RCT_EXPORT_METHOD(stopAvoiding)
{
[_bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, RCTSparseArray *viewRegistry) {
[IHKeyboardAvoiding removeAll];
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment