6

Is there a way to set a WebView in Xamarin to scale its content to fit the screen by default and still allow for pinch zooming in/out?

We're going to use this to display documents we have online.

jbassking10
  • 833
  • 4
  • 15
  • 42

2 Answers2

9

I solved the scale fit to page and zooming by Custom Renderers, as given below

For IOS

 public class CustomWebViewRenderer : WebViewRenderer
{
    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);

        var view = Element as CustomWebView;
        if (view == null || NativeView == null)
        {
            return;
        }
        this.ScalesPageToFit = true;
    }

}

For Android

public class CustomWebViewRenderer : WebViewRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
            Control.Settings.BuiltInZoomControls = true;
            Control.Settings.DisplayZoomControls = false;

            Control.Settings.LoadWithOverviewMode = true;
            Control.Settings.UseWideViewPort = true;
        }
    }
}
rsssssonsss
  • 413
  • 5
  • 6
  • that's very helpful. Can something be done when the phone is in landscape(horizontal) view? It requires scroll to view the whole content. Thank you – ehem Nov 04 '21 at 19:16
-1

If you're using Xamarin.Forms that would look something like this

<?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="WebViewDemo.LoadingDemo" Title="Loading Demo">
    <ContentPage.Content>
    <StackLayout>
      <WebView x:Name="Browser"
      VerticalOptions = LayoutOptions.FillAndExpand ,
      HorizontalOptions = LayoutOptions.FillAndExpand,
    </StackLayout>
    </ContentPage.Content>
</ContentPage>

This should fill the page and not affect pinch and zoom.

xerotolerant
  • 1,955
  • 4
  • 21
  • 39
  • 1
    Thanks but what I'm looking for is a way for the WebView to fit the page being loaded to the WebView. The way it is now, I have to pan around the screen to see the entire page being loaded. – jbassking10 Dec 15 '16 at 02:45
  • Ohh. I didn't get that reading your quesiton. Have you tried a cusotom renderer like recommended here? https://forums.xamarin.com/discussion/18113/webview-zoom – xerotolerant Dec 15 '16 at 03:06
  • No. I was hoping there was a new way by now but I guess not. It looks like that's what I'll be doing. Thanks. – jbassking10 Dec 15 '16 at 03:35