'How to make WebView scrollable in react-native-webview?
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?
Solution 1:[1]
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
Solution 2:[2]
Just add nestedScrollEnabled=true
props i webview component
<ScrollView
<WebView nestedScrollEnabled source={{uri:'https://www.website.com' }} />
</ScrollView>
Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | MohamadKh75 |
Solution 2 | Mustafa Uysal |
Solution 3 | illusionist |