React native: webview height

前端 未结 8 1134
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 18:26

Hi I know it\'s a known issue about the auto height of webview in react native, and I have tried all the possibles solutions I\'ve found on the internet such as :

ht

8条回答
  •  一向
    一向 (楼主)
    2020-12-01 18:54

    I just follow this guide: https://github.com/react-native-community/react-native-webview/blob/master/docs/Guide.md#communicating-between-js-and-native and succeeded in my work. Here is solution: 1. Define script to send document height to native env after loaded website. 2. Handle onMesssage of webview component and reset Height via state.

    const webViewScript = `
      setTimeout(function() { 
        window.postMessage(document.documentElement.scrollHeight); 
      }, 500);
      true; // note: this is required, or you'll sometimes get silent failures
    `;
    
    
    ...
    constructor(props) {
        super(props);
        this.state = {
          webheight:100,
        }
    
    ...
    
    "}}
      onMessage={event => {
        this.setState({webheight: parseInt(event.nativeEvent.data)});
      }}
      javaScriptEnabled={true}
      injectedJavaScript ={webViewScript}
      domStorageEnabled={true}
    >
    

    Hope that help!

提交回复
热议问题