ios - react 原生渲染原生自定义 View
<p><p>我关注 <a href="http://facebook.github.io/react-native/docs/native-components-ios.html#content" rel="noreferrer noopener nofollow">this doc</a>但真的不明白如何在js端呈现我的客户 View 。</p>
<p>我使用 Storyboard生成一个简单的 View 并将其分配给我的 <code>CustomView</code> 类,该类继承了 <code>UIView</code>。</p>
<p> <a href="/image/Aevtb.jpg" rel="noreferrer noopener nofollow"><img src="/image/Aevtb.jpg" alt="enter image description here"/></a> </p>
<p>然后我像下面这样写 <code>MyCustomViewManager.m</code></p>
<pre><code>#import "RCTViewManager.h"
#import "CustomView.h"
@interface MyCustomViewManager : RCTViewManager
@end
@implementation MyCustomViewManager
RCT_EXPORT_MODULE()
- (UIView *)view
{
return [ init];
}
@end
</code></pre>
<p>最后我在下面写了js端文件<code>index.ios.js</code>。</p>
<pre><code>import React from 'react-native';
const {AppRegistry, View, requireNativeComponent,} = React;
class Demo extends React.Component {
constructor(){
super();
}
var CustomView = requireNativeComponent('CustomView', null);
render() {
return (
<View>
<CustomView></CustomView>
</View>
);
}
}
AppRegistry.registerComponent('Demo', () => Demo);
</code></pre>
<p>也许我做错了什么,我不明白官方文档关于以下代码的意思
<code>module.exports = requireNativeComponent('RCTMap', null);</code></p>
<p>如何在 requireNativeComponent 方法中表示原生端 <code>CustimView</code>?能给我看看代码吗,谢谢..</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>此行表示您纯粹导出 RCTMap 而不处理任何自定义属性</p>
<pre><code>module.exports = requireNativeComponent('RCTMap', null);
</code></pre>
<p>正如 React Native Docs 所说,</p>
<pre><code>import { requireNativeComponent } from 'react-native';
// requireNativeComponent automatically resolves this to "RCTMapManager"
module.exports = requireNativeComponent('RCTMap', null);
</code></pre>
<p>so <code>requireNativeComponent</code> 接收两个参数,第一个参数是您要从之前的桥接过程中导入的组件的名称,第二个参数是处理 native 组件的类,这是用于操作自定义组件中的属性和一些逻辑,就像那样 </p>
<pre><code>// MapView.js
import React from 'react';
import { requireNativeComponent } from 'react-native';
class MapView extends React.Component {
render() {
return <RCTMap {...this.props} />;
}
}
MapView.propTypes = {
/**
* When this property is set to `true` and a valid camera is associated
* with the map, the camera’s pitch angle is used to tilt the plane
* of the map. When this property is set to `false`, the camera’s pitch
* angle is ignored and the map is always displayed as if the user
* is looking straight down onto it.
*/
pitchEnabled: React.PropTypes.bool,
};
var RCTMap = requireNativeComponent('RCTMap', MapView);
module.exports = MapView;
</code></pre>
<p>希望这对关心它的人有用</p>
<p>请查看<a href="https://facebook.github.io/react-native/docs/native-components-ios.html#properties" rel="noreferrer noopener nofollow">React Native Docs</a> </p></p>
<p style="font-size: 20px;">关于ios -react 原生渲染原生自定义 View ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34815679/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34815679/
</a>
</p>
页:
[1]