How to fix WebView Custom Renderer rotation issues on iOS?

I need a WebView custom renderer. I have been able to create a custom renderer that works; however, whenever the device is rotated on iOS, the webpage is not resized to fit the changes in width and height. If I use the built in Forms' WebView, the page resizes correctly. What am I doing incorrectly with this custom renderer?

Below is a stripped down version of my custom renderer (the issue still occurs):

using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using UIKit;
using Foundation;

[assembly: ExportRenderer(typeof(WebView), typeof(MyProject.iOS.WebViewRenderer))]
namespace MyProject.iOS {
    public class WebViewRenderer : ViewRenderer<WebView, UIWebView> {
        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e) {
            if (Control == null) {
                var webView = new UIWebView(Frame);
                webView.AutoresizingMask = UIViewAutoresizing.All;
                SetNativeControl(webView);
                webView.LoadRequest(new NSUrlRequest(new NSUrl("http://cnn.com")));
            }
        }
    }
}

Best Answer

Answers

  • Thank you, sir. You just save my day.
    Your solution also fits with the case callback to the previous webview.

Sign In or Register to comment.