ReactNative在ScrollView中嵌套使用WebView,WebView自适应高度

参考http://blog.csdn.net/chichengjunma/article/details/53423689

使用发现高度计算的不对,经过排查和测试,正确地如下:

1、新建文件WebViewAutoHeight.js

/**

* WebView自适应高度

*/

importReact, {Component,propTypes}from'react';

import{WebView, View, Text}from"react-native";

constBODY_TAG_PATTERN=/\<\/ *body\>/;

// Do not add any comments to this! It will break because all line breaks will removed for

// some weird reason when this script is injected.

varscript=`

;(function() {

var wrapper = document.createElement("div");

wrapper.id = "height-wrapper";

while (document.body.firstChild) {

wrapper.appendChild(document.body.firstChild);

}

document.body.appendChild(wrapper);

var i = 0;

function updateHeight() {

document.title = wrapper.clientHeight;

window.location.hash = ++i;

}

updateHeight();

window.addEventListener("load", function() {

updateHeight();

setTimeout(updateHeight, 1000);

});

window.addEventListener("resize", updateHeight);

}());

`;

conststyle=`

body,html, #height-wrapper {

margin: 10;

padding: 0;

}

#height-wrapper {

position: absolute;

top: 0;

left: 0;

right: 0;

}

${script}

`;

constcodeInject= (html) => html.replace(BODY_TAG_PATTERN,style+"");

/**

* Wrapped Webview which automatically sets the height according to the

* content. Scrolling is always disabled. Required when the Webview is embedded

* into a ScrollView with other components.

*

* Inspired by this SO answer http://stackoverflow.com/a/33012545

* */

varWebViewAutoHeight= React.createClass({

propTypes: {

source: React.PropTypes.object.isRequired,

injectedJavaScript: React.PropTypes.string,

minHeight: React.PropTypes.number,

onNavigationStateChange: React.PropTypes.func,

style: WebView.propTypes.style,

},

getDefaultProps() {

return{minHeight:0};

},

getInitialState() {

return{

realContentHeight:this.props.minHeight,

};

},

handleNavigationChange(navState) {

if(navState.title) {

constrealContentHeight= navState.title;//parseInt(navState.title, 20) || 0; // turn NaN to 0

if(!isNaN(realContentHeight)){

this.setState({realContentHeight});

}

}

if(typeof this.props.onNavigationStateChange==="function") {

this.props.onNavigationStateChange(navState);

}

},

render() {

const{source,style,minHeight, ...otherProps} =this.props;

consthtml=source.html;

if(!html) {

throw newError("WebViewAutoHeight supports only source.html");

}

if(!BODY_TAG_PATTERN.test(html)) {

throw newError("Cannot find from: "+html);

}

return(

{...otherProps}

source={{html:codeInject(html)}}

scrollEnabled={false}

style={[style, {height:Math.max(this.state.realContentHeight,minHeight)}]}

javaScriptEnabled

onNavigationStateChange={this.handleNavigationChange}

/>

);

},

});

export defaultWebViewAutoHeight;

2、使用

import WebViewAutoHeight from '../components/WebViewAutoHeight';

constructor(props) {

super(props);//这一句不能省略,照抄即可

this.state= {

height:0,

};

}

使用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 现在最热门的前端框架,毫无疑问是 React 。上周,基于 React 的 React Native 发布,结果一...
    sakura_L阅读 446评论 0 0
  • 原教程内容详见精益 React 学习指南,这只是我在学习过程中的一些阅读笔记,个人觉得该教程讲解深入浅出,比目前大...
    leonaxiong阅读 2,862评论 1 18
  • 今天天变了脸,虽然有天气预报,但是昨天的摄氏二十以上和今天的十度以下的大反差还是让人很不舒服,收起的厚棉衣又得拿出...
    窦家香阅读 115评论 0 0
  • 1 一个大叔,他从05年就开始写作,那一年33岁。每天写,每天坚持 写1000字。 刚开始只是写日记,开始写日记时...
    Ameizeng阅读 402评论 2 10
  • cosplay爱好者如何成为职业coser (连载9) ——如何用创业的角度打造自己的专属品牌,实现独立coser...
    早叔阅读 1,579评论 0 0