I want to create a simple UIView subclass and then make it available as a React Native JavaScript component via bridging to React Native. I have followed these directions and thumbed through lots of the react source code: https://facebook.github.io/react-native/docs/native-components-ios.html
Unfortunately, I don't know where my React Native component is failing. This is my Obj-C manager class:
// header
#import "RCTViewManager.h"
#import "ColorPicker.h"
@interface ColorPickerManager : RCTViewManager
@end
// implementation
#import "ColorPickerManager.h"
@interface ColorPickerManager()
@property (nonatomic) ColorPicker * colorPicker;
@end
@implementation ColorPickerManager
RCT_EXPORT_MODULE()
- (instancetype)init {
self = [super init];
if ( self ) {
NSLog(@"color picker manager init");
self.colorPicker = [[ColorPicker alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
}
return self;
}
- (UIView *)view {
NSLog(@"color picker manager -view method");
return self.colorPicker;
}
@end
Here is my simple UIView subclass that I vend via the above -view method:
// header
#import <UIKit/UIKit.h>
@interface ColorPicker : UIView
@end
// implementation
#import "ColorPicker.h"
@interface ColorPicker()
@property (nonatomic) NSArray * colors;
@end
@implementation ColorPicker
- (instancetype)init {
NSLog(@"init");
self = [super init];
if ( self ) {
[self setUp];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
NSLog(@"init with frame: %@", NSStringFromCGRect(frame));
self = [super initWithFrame:frame];
if ( self ) {
[self setUp];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
NSLog(@"init with coder: %@", aDecoder);
self = [super initWithCoder:aDecoder];
if ( self ) {
[self setUp];
}
return self;
}
- (void)setUp {
self.colors = @[[UIColor redColor], [UIColor greenColor], [UIColor blueColor]];
self.backgroundColor = self.colors[0];
}
- (void)layoutSubviews {
NSLog(@"layout subviews");
}
@end
Finally, here's my react component being bridged over to JavaScript:
var { requireNativeComponent } = require('react-native');
var ColorPicker = requireNativeComponent('ColorPicker', null);
module.exports = ColorPicker;
debugger;
And it's declaration and render to the screen:
'use strict';
var React = require('react-native');
var ColorPicker = require('./BridgedComponents/ColorPicker.js');
var {
StyleSheet
} = React;
var iOS = React.createClass({
render: function() {
debugger;
return (
<ColorPicker style={styles.container} />
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});
AppRegistry.registerComponent('iOS', () => iOS);
This results in nothing rendering to the screen when I expect to see a 100x100 red square. What I have tried:
UIView will render exactly what I expect if I remove all React Native code from the project and by putting this view as a subview of a different rootViewController.ColorPicker in my main .js file is not undefined at the time its rendered to screen and when its created via debugger. height: 100, width: 100 to the rendered ColorPicker but it still doesn't show a red squARE_Questions for RN Gurus_
Any help/advice will be greatly appreciated.
Apparently whatever UIView instance is vended by the -view method of the "manager" class React Native mucks with its background and frame. I was able to solve this by wrapping my small 100x100 UIView in another view altogether. This is not obvious at all.
Ran into the same glitch.
@facebook-github-bot answered
Closing this issue as @browniefed says the question asked has been answered. Please help us by asking questions on StackOverflow. StackOverflow is amazing for Q&A: it has a reputation system, voting, the ability to mark a question as answered. Because of the reputation system it is likely the community will see and answer your question there. This also helps us use the GitHub bug tracker for bugs only.
If you could submit a PR to improve the documentation so that others would not run into this same issue that would be fantastic.
@browniefed my solution is ~6 months old. Do you think it is still viable? Also, I'm not certain where I would submit the PR or what it should look like. Comments on the manager class? Comments in the documentation?
@chefnobody If it applies probably just a small mention in the http://facebook.github.io/react-native/docs/native-components-ios.html#content docs. However it's up to you if you feel like the info is valuable :), no worries if you don't.
@browniefed https://github.com/facebook/react-native/pull/6786. Thoughts?
@chefnobody can u explain me clearly. I can't understand clearly how could you use the -view method.
@chefnobody How did you wrap your square in another UIView exactly?
Did you add it as a subview?
Instead of returning my red square UIView in the -view method, I added that red square UIView as a subview to _another_ UIView instance, let's call it parent. Then I returned parent from the -view method. Does that make more sense?
Just as I said.
Many Thanks Aaron @chefnobody .
Most helpful comment
Instead of returning my red square
UIViewin the-viewmethod, I added that red squareUIViewas a subview to _another_UIViewinstance, let's call itparent. Then I returnedparentfrom the-viewmethod. Does that make more sense?