Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native: webview height

Tags:

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 :

https://gist.github.com/epeli/10c77c1710dd137a1335

https://github.com/danrigsby/react-native-web-container/blob/master/index.js

and all the solutions suggested in: React native: Is it possible to have the height of a html content in a webview?

But unfortunately none of these seems to work for me, I understand that the workaround they all suggest is to set the title to the height, but in my case it seems that the title always stays the same which is : "text/html ...." and the rest of my html. I get the html content from an API, it comes without a body, head or html tags, I've also tried adding these tags manually to the html and nothing seems to work.

I would love to hear if anyone else had that problem and how did it get fixed.

like image 762
Matan Kadosh Avatar asked Feb 17 '16 01:02

Matan Kadosh


People also ask

How do I set the height of Webview in react native?

React Native does not provide dynamic height to Webview Component, so we need to provide fix height to our WebView but fix height is not suitable for the Html content. If we use Webview with some other View Component then Webview takes all screen size and other views are not displayed.

How do you auto height in react native?

To set image width to be 100% and height to be auto in React Native, we can set the width and height of the Image . to call Dimensions. get with 'window' to get the window's dimension. Then we calculate the ratio between the width and height of the image with win.

How do I change dynamic height in react native?

In react-native, to set the dynamic width and height to a component, we use built-in interface Dimensions. it takes the dimension of a given component and then sets the width and height to its equivalent.


2 Answers

I wrap WebView inside a View, and set the height from the View.

<View style={{ height: 200 }}>   <WebView     automaticallyAdjustContentInsets={false}     source={{uri: 'https://player.vimeo.com/video/24156534?title=0&byline=0&portrait=0'}}   /> </View> 
like image 76
haris Avatar answered Nov 15 '22 11:11

haris


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,     }  ...  <WebView style={{height: this.state.webheight}}   automaticallyAdjustContentInsets={false}   scrollEnabled={false}   source={{uri: "http://<your url>"}}   onMessage={event => {     this.setState({webheight: parseInt(event.nativeEvent.data)});   }}   javaScriptEnabled={true}   injectedJavaScript ={webViewScript}   domStorageEnabled={true} ></WebView> 

Hope that help!

like image 31
Tung Nguyen Avatar answered Nov 15 '22 11:11

Tung Nguyen