React Native封装了一些常用的原生组件譬如ScrollView和TextInput,有的时候我还需要使用其他的原生组件譬如一些第三方组件或自己写的组件,而这些组件React Native是没有替我们封装好的,但是React Native提供了一套封装和植入原生组件的方案,这个方案非常简单。
步骤如下:
1.选择一个已有的原生视图,这里我在xcode里新建一个视图MyCustomView,在MyCustomView上加入一个label来标记这是自己创建的视图代码如下:
MyCustomView.h
#import <UIKit/UIKit.h>
@interface MyCustomView : UIScrollView
@end
MyCustomView.m
#import "MyCustomView.h"
@implementation MyCustomView
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self!=nil) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
label.text = @"你好中国";
[self addSubview:label];
label.textColor = [UIColor blueColor];
}
return self;
}