ReactNative在ScrollView中嵌套使用WebView,WebView自適應高度

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

使用發(fā)現(xiàn)高度計算的不對,經(jīng)過排查和測試,正確地如下:

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,

};

}

使用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • 現(xiàn)在最熱門的前端框架,毫無疑問是 React 。上周,基于 React 的 React Native 發(fā)布,結(jié)果一...
    sakura_L閱讀 440評論 0 0
  • 原教程內(nèi)容詳見精益 React 學習指南,這只是我在學習過程中的一些閱讀筆記,個人覺得該教程講解深入淺出,比目前大...
    leonaxiong閱讀 2,860評論 1 18
  • 今天天變了臉,雖然有天氣預報,但是昨天的攝氏二十以上和今天的十度以下的大反差還是讓人很不舒服,收起的厚棉衣又得拿出...
    竇家香閱讀 112評論 0 0
  • 1 一個大叔,他從05年就開始寫作,那一年33歲。每天寫,每天堅持 寫1000字。 剛開始只是寫日記,開始寫日記時...
    Ameizeng閱讀 394評論 2 10
  • cosplay愛好者如何成為職業(yè)coser (連載9) ——如何用創(chuàng)業(yè)的角度打造自己的專屬品牌,實現(xiàn)獨立coser...
    早叔閱讀 1,563評論 0 0