WebView只要设置了高度就可以正常滚动,所以我们可以在URL里面加hash的方法让react得到WebView的高度。
我的情况是WebView是HTML代码,如果是URL类似:
WebView内代码:
window.onload = function(){
var height = document.body.clientHeight;
window.location.hash = '#' + height;
}
React:
source={{ html: your_html }}
style={{ height: this.state.WebViewHeight }}
onNavigationStateChange={(info)=>{
this.setState({
WebViewHeight:info.url.replace('about:blank%23','')/1
})
}}
/>
页面加载后,webview内的JS代码获取自己的高度,添加一个hash给自己的URL,React当感知到webview加载状态变化后,读取这个hash值,传递给this.state.WebViewHeight,从而改变WebView的高度。