How to make WebView scrollable in react-native-webview?
Asked Answered
D

3

6

I have <WebView> that is embedded in a <Card.Content> as I am using react-native-paper. I am trying to have WebView scrollable. The webview shows but not scrollable.

Code I am using:

  <Card theme={{ roundness: 20 }} style={{ margin: 10, height: height*0.8 }} elevation={14}>
     <Card.Content style={{ flex: 1 }}>
        <View style={{ justifyContent: "center", alignItems: "center" }}>
              <WebView
              style={{ width: width * 0.8, height: height * 0.9 }}
              allowsBackForwardNavigationGestures={false}
              source={{ uri: "https://paypal.com"}}
              pullToRefreshEnabled={false}
              javaScriptEnabled
              startInLoadingState
              decelerationRate="normal"
              scalesPageToFit={Platform.OS == "android"?  false : true}
              originWhitelist={['*']}
              domStorageEnabled={true}
              scrollEnabled
              renderError={e => { console.log(e); }}
              />
        </View>
     </Card.Content>
  </Card>

How can I make WebView scrollable?

Dub answered 24/8, 2020 at 21:33 Comment(0)
A
11

Just add nestedScrollEnabled=true props i webview component

<ScrollView
    <WebView nestedScrollEnabled source={{uri:'https://www.website.com' }} />
 </ScrollView>
Acquiescent answered 1/3, 2022 at 10:32 Comment(0)
H
2

I guess you can try to wrap your WebView inside a ScrollView like this:

export default class App extends Component {
  render() {
    return (
      <ScrollView contentContainerStyle={{ flexGrow: 1 }}>
        <WebView
          source={{
            uri:
              'https://github.com/react-native-community/react-native-webview/issues/22'
          }}
          style={{ height: 100 }}
        />

        <WebView
          source={{ uri: 'https://bbc.co.uk/news' }}
          style={{ height: 100 }}
        />
      </ScrollView>
    );
  }
}

Also, you can use react-native-autoheight-webview

Harrow answered 24/8, 2020 at 21:48 Comment(5)
can you please explain why you are using two webviews in scrollview? I did not get it.Dub
I tried your method but did not work. I will give a try the library you mentioned.Dub
I added two webviews just for testing!Harrow
I found the issue thank you for helping, the issue was that I was using margin on <Card> view. I removed the margin and I got it scrollable now. Not sure why margin affected the <WebView> scrolling.Dub
If your question has been answered, please make sure to accept and vote up an answer for further references.Harrow
A
1

I was stuck with this issue.

Problem definition

Webview was inside ScrollView and Webview was taking height greater than ScrollView thats why the ScrollView was stealing the scroll event and I was not able to scroll through the webpage.

Solution

Make Webview fit the Scrollview height. For this we need to use flexGrow in contentContainerStyle

Simple Example
   return (
    <SafeAreaView style={{flex: 1}}>
      <ScrollView
        style={{ width: '100%' }}
        contentContainerStyle={{ flexGrow: 1 }}
      >
        <WebView source={{ uri: contentUrl }} originWhitelist={['*']} />
      </ScrollView>
    </SafeAreaView>
  );

{flex: 1} and { width: '100%' } are important otherwise content will not be visible.

Ataraxia answered 3/12, 2021 at 0:8 Comment(1)
Thank you for the width 100%. How is some software this dumb?Vicinal

© 2022 - 2024 — McMap. All rights reserved.