简体   繁体   English

React Native动态webview高度

[英]React Native dynamic webview height

I have WebView content are which changes it's height depending on amount of content. 我有WebView内容,它根据内容的数量改变它的高度。 So I found a way how to take the height of the content through the document.title attribute onNavigationStateChange. 所以我找到了一种如何通过document.title属性onNavigationStateChange来获取内容高度的方法。 Looks like this: 看起来像这样:

let html = '<!DOCTYPE html><html><body><script>document.title = document.height;</script></body></html>';

And on onNavigationStateChange 并在onNavigationStateChange上

onNavigationStateChange(navState) {
  this.setState({
    height: navState.title
  });
  console.log("DATA");
  console.log(this.state.height)
}

On the actual WebView render I do: 在实际的WebView渲染上我做:

<WebView style={this._setWebviewHeight()}
           automaticallyAdjustContentInsets={false}
           scrollEnabled={false}
           onNavigationStateChange={this.onNavigationStateChange.bind(this)}
           html={html}>
</WebView>

Where: 哪里:

_setWebviewHeight() {
 return {
  height: this.state.height,
  padding: 20,
 }
}

And in this case I'm getting error and I am not able to get the height of content through state. 在这种情况下,我收到错误,我无法通过状态获得内容的高度。 What shall I do then? 那我该怎么办?

The idea of solution is taken from here: React native: Is it possible to have the height of a html content in a webview? 解决方案的想法来自于: React native:是否可以在webview中获得html内容的高度?

I have found the solution in the implementation of one of the RN third party libraries. 我在其中一个RN第三方库中找到了解决方案。 Here is the link to it: https://github.com/danrigsby/react-native-web-container/blob/master/index.js 以下是它的链接: https//github.com/danrigsby/react-native-web-container/blob/master/index.js

With this kind of approach all worked good. 通过这种方法,一切都很顺利。

试试这个lib: https//github.com/iou90/react-native-autoheight-webview它适用于iOS和Android

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM