Ravior
Ravior

Reputation: 611

Webview can't scroll

I've got this Xamarin Forms page:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:TestApp1"
             x:Class="TestApp1.MainPage">
<ContentPage.Content>
  <StackLayout Orientation="Vertical">
    <WebView Source="http://www.google.de" HeightRequest="3000" WidthRequest="100"/>
  </StackLayout>
</ContentPage.Content>
</ContentPage>

When I open my app, enter anything in the google prompt, I can't scroll on the results page. How do I enable this?

When I google for "xamarin webview enable scrolling" I only find information about disabling it...

Upvotes: 6

Views: 6863

Answers (4)

This works for me in android very well

[assembly: ExportRenderer(typeof(MyWebView),typeof(MyWebViewRenderer))]
namespace App.Droid.Renderers
{
    class MyWebViewRenderer : WebViewRenderer
    {
        public MyWebViewRenderer(Context context) : base(context)
        {
        }
 
        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);
        }
 
        public override bool DispatchTouchEvent(MotionEvent e)
        {
            Parent.RequestDisallowInterceptTouchEvent(true);
            return base.DispatchTouchEvent(e);
        }
    }
}

Upvotes: 0

David Wilkins
David Wilkins

Reputation: 56

For me the issue was inside the <WebView/>, I had set a static HeightRequest and WidthRequest - because the documentation said those were required if you used a <StackLayout> - but this wasn't true

Deleting those and adding the VerticalOptions="FillAndExpand" and HorizontalOptions="FillAndExpand" , solved the problem for me and allowed me to scroll as expected.

<WebView VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Source="https://google.com/"/>

Upvotes: 3

Ashi
Ashi

Reputation: 864

Create your own renderer and override this method as follows:

public override bool DispatchTouchEvent(MotionEvent e)
{
    Parent.RequestDisallowInterceptTouchEvent(true);
    return base.DispatchTouchEvent(e);
}

Please find details in this link

Upvotes: 6

Yuri S
Yuri S

Reputation: 5370

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ButtonRendererDemo.WebViewPage">

  <ContentPage.Content>
    <StackLayout Orientation="Vertical" Padding="10" Spacing="10">
      <Button Text="Press me" HorizontalOptions="CenterAndExpand" VerticalOptions="Center" BackgroundColor="Green"/>        
      <WebView Source="http://www.google.de" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
    </StackLayout>
  </ContentPage.Content>    
</ContentPage>

enter image description here

Upvotes: 3

Related Questions