Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Android WebView How to disable horizontal scroll?

I have a problem with webview and scrolling. I want to disable all scrolling. I used scrollEnabled, for IOS it works fine. But for android no property like this exists. I tried a lot of scripts but nothing works.

At first look everything seems ok but if user swipes to right side then the text/div moves outside of screen.

how it looks like when it is ok

and this is what happened

how it looks like when user will swipe to side

this is my webview:

<WebView
  bounces={false}
  ref={(element) => { this.webViewRef = element; }}
  javaScriptEnabled
  scalesPageToFit={false}
  onShouldStartLoadWithRequest={this._onShouldStartLoadWithRequest}
  style={{
    height: this.state.webViewHeight,
  }}
  scrollEnabled={false}
  source={{
    html: `
          <!DOCTYPE html>
          <html>
            <head>${injectedStyle(platformStyles.fontSizes.base || 16, webViewWidth)}</head>
            <body>
              <div id="baseDiv" class="ex1">${content}</div>
              ${injectedJS}
            </body>
          </html>
    `,
    baseUrl: 'http://s3.eu-central-1.amazonaws.com',
  }}
  onNavigationStateChange={this._processNavigationStateChange}
  automaticallyAdjustContentInsets={false}
/>

and i tried to add a style like this ( overflow: 'hidden' but its doesn't work):

    const injectedStyle = (fontSize, widhtPlatform) => `<style type="text/css">
    ...
    body {
            display: flex;
            overflow: hidden;
            background-color: orange;
    ...
        }
    div.ex1 {
      overflow: hidden;
  background-color: coral;
  border: 1px solid blue;
    ...
    }
    </style>`;

and JS to disable scroll:

const injectedJS = `
  <script>
    var scrollEventHandler = function()
      {
        window.scroll(0, window.pageYOffset)
      }
    document.addEventListener('scroll', scrollEventHandler, false);
    document.getElementById('scrollbar').style.display = 'block';
    document.body.style.overflow = 'hidden';

  </script>
`;

We use RN:

react-native-cli: 2.0.1

react-native: 0.53.3

like image 993
Lukáš Šálek Avatar asked Mar 07 '18 15:03

Lukáš Šálek


People also ask

How do I disable horizontal scrolling in react native WebView?

For Android change scalesPageToFit={false} into scalesPageToFit={true} . It will work.

How do I disable scrolling in WebView?

If you subclass Webview, you can simply override onTouchEvent to filter out the move-events that trigger scrolling. public class SubWebView extends WebView { @Override public boolean onTouchEvent (MotionEvent ev) { if(ev. getAction() == MotionEvent. ACTION_MOVE) { postInvalidate(); return true; } return super.

How do I make WebView scrollable in react native?

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


1 Answers

For Android change scalesPageToFit={false} into scalesPageToFit={true}. It will work.

// ....
const scalesPageToFit = Platform.OS === 'android';

// .... 
return 
    <WebView
      scalesPageToFit={scalesPageToFit}
      bounces={false}
      scrollEnabled={false}
// ....

Update: for recent versions of rn-webview depending on the html content you might need to use also some extra css to target the content that has problems:

max-width: 100%;

overflow-x: hidden;

and/or showsHorizontalScrollIndicator={false}

like image 165
Florin Dobre Avatar answered Oct 24 '22 19:10

Florin Dobre