Created
August 18, 2015 15:55
-
-
Save mbrock/8ebb6b723f3185ee3c26 to your computer and use it in GitHub Desktop.
This file contains 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
// 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