在iOS原生功能中加入RN,请看之前 写的 RN与iOS交互系列文章.本篇只讲下视图混编.

关键点只有二:

1.通过 RCTRootView 加载RN视图.

2.RN中,只需要AppRegistry.registerComponent()导出即可.

关键代码:

OC里面:

//
// RootViewController.m
// iOSRN
//
// Created by Shaoting Zhou on 2017/12/8.
// Copyright © 2017年 Shaoting Zhou. All rights reserved.
// #import "RootViewController.h"
#import <React/RCTRootView.h>
#define Main_Screen_Height [[UIScreen mainScreen] bounds].size.height
#define Main_Screen_Width [[UIScreen mainScreen] bounds].size.width
@interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad {
[self loadIOSView];
[self loadRNView]; }
#pragma mark - 添加IOS视图
-(void)loadIOSView{
self.view.backgroundColor = [UIColor redColor]; NSString * strUrl = @"http://localhost:8081/App.bundle?platform=ios&dev=true";
NSURL * jsCodeLocation = [NSURL URLWithString:strUrl];
RCTRootView * rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"iosrn"
initialProperties:nil
launchOptions:nil];
rootView.frame = CGRectMake(, , , ); [self.view addSubview:rootView]; } #pragma mark - 添加rn视图
-(void)loadRNView{
[super viewDidLoad];
NSString * strUrl = @"http://localhost:8081/index.bundle?platform=ios&dev=true";
NSURL * jsCodeLocation = [NSURL URLWithString:strUrl]; RCTRootView * rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"iosrn"
initialProperties:nil
launchOptions:nil];
rootView.frame = CGRectMake(, Main_Screen_Height/, Main_Screen_Width, Main_Screen_Height/); UILabel * la = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
la.backgroundColor = [UIColor blueColor];
la.text = @"我是原生";
[rootView addSubview:la]; [self.view addSubview:rootView]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end

RN里面:

import React, { Component } from 'react';
import {
AppRegistry,
Platform,
StyleSheet,
Text,
View
} from 'react-native'; const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' +
'Cmd+D or shake for dev menu',
android: 'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
}); export default class App extends Component<{}> {
render() {
return (
<View style={styles.container}>
<Text>
我是RN
</Text>
</View>
);
}
} const styles = StyleSheet.create({
container: {
flex: ,
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: ,
textAlign: 'center',
margin: ,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: ,
},
}); AppRegistry.registerComponent('iosrn', () => App);

效果如图:

github:  https://github.com/pheromone/IOS-native-and-React-native-interaction  中的 iOSRN

iOS原生 和 react native视图混编的更多相关文章

  1. 一个资深iOS开发者对于React Native的看法

    一个资深iOS开发者对于React Native的看法 当我第一次尝试ReactNative的时候,我觉得这只是网页开发者涉足原生移动应用领域的歪门邪道.   我认为一个js开发者可以使用javasc ...

  2. iOS 写给iOS开发者的React Native学习路线(转)

    我是一名iOS开发者,断断续续一年前开始接触React Native,最近由于工作需要,专职学习React Native也有一个多月了.网络上知识资源非常的多,但能让人豁然开朗.迅速学习的还是少数,我 ...

  3. 写给iOS开发者的React Native学习路线(转)

    我是一名iOS开发者,断断续续一年前开始接触React Native,最近由于工作需要,专职学习React Native也有一个多月了.网络上知识资源非常的多,但能让人豁然开朗.迅速学习的还是少数,我 ...

  4. 现有iOS项目集成React Native过程记录

    在<Mac系统下React Native环境搭建>配置了RN的开发环境,然后,本文记录在现有iOS项目集成React Native的过程,官方推荐使用Cocoapods,项目一开始也是使用 ...

  5. 原生工程接入Flutter实现混编

    前言 上半年我定的OKR目标是帮助团队将App切入Flutter,实现统一技术栈,变革成多端融合开发模式.Flutter目前是跨平台方案中最有潜力实现我们这个目标的,不管是Hybird还是React ...

  6. [转] 一个资深iOS开发者对于React Native的看法

    当我第一次尝试ReactNative的时候,我觉得这只是网页开发者涉足原生移动应用领域的歪门邪道. 我认为一个js开发者可以使用javascript来构建iPhone应用确实是一件很酷的事情,但是我很 ...

  7. Android原生嵌入React Native

    1.首先集成的项目目录 我使用的是直接按照react-native init Project 的格式来导入的,也就是说,我的Android项目目录是跟node_modules是在一个目录下的. 我们i ...

  8. 《React Native 精解与实战》书籍连载「iOS 平台与 React Native 混合开发」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

  9. 【React Native】在原生和React Native间通信(RN调用原生)

    一.从React Native中调用原生方法(原生模块) 原生模块是JS中也可以使用的Objective-C类.一般来说这样的每一个模块的实例都是在每一次通过JS bridge通信时创建的.他们可以导 ...

随机推荐

  1. 区间DP 洛谷P2858牛奶零食

    题目链接 题意:你有n个货物从1-n依次排列,每天可以从两侧选一个出来卖,卖的价格是当天的天数乘该货物的初始价格,问这批货物卖完的最大价格 输入:第一行n,之后是n个货物的初始价值 这道题不能用贪心做 ...

  2. Activiti之流程通过、驳回、会签、转办、中止、挂起等核心操作封装(Activiti5.9)

    http://blog.csdn.net/rosten/article/details/38300267 package com.famousPro.process.service.impl; imp ...

  3. 【二分查找】 跳石头NOIP2015提高组 D2T1

    [二分查找]跳石头NOIP2015提高组 D2T1 >>>>题目 [题目描述] 一年一度的“跳石头”比赛又要开始了! 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石 ...

  4. php 类名和方法名相同(构造函数)

    //php 5.6class father{ public function __construct() { echo __METHOD__; }} class son extends father{ ...

  5. vue-文字块收缩与展开功能

    在设计图中要求的效果为: 文字限制超过9行即隐藏,并显示“展开”按钮,点击按钮进行切换,控制文本全部展示和部分展示 在原本的实现过程中,使用了红框内的判断方式: 页面代码: 样式则规定嵌套元素给一个死 ...

  6. java ssl 连接AD域

    1.安装证书服务和IIS服务,不需要启动asp.(其会在c盘生成一个crt文件)2.获取crt证书文件 windows 2003:文件位于系统盘根目录 windows 2008: 文件位于C:\Win ...

  7. 使用layer的弹窗时,出现layer引入成功,触发成功,控制台无报错,但是页面无变化或者仅出现遮罩层的问题的解决思路

    ------------------------------------------20180410补充------------------------------------------------ ...

  8. java按照指定格式输出系统时间

    public class TimeFour { public static void main(String[] args) throws ParseException{ TimeFour four ...

  9. 生成不同尺寸dimen的xml文件以及文件夹

    import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import j ...

  10. [tour]2019HUST onsite签到

    先定一个小目标,从签到题开始讲清楚 虽然因为我喜欢签到题的气球导致签到题并没有行使责任.. F.Mesh 和某CF题(我找不到了)完 全 一 致,由于某些玄学原因没有get到(orz谢罪) 给出一个6 ...