Forum Xamarin Xamarin.iOS

Xamarin Form IOS WKWebView Pinch Zoom

KhairulFLPMKhairulFLPM Member ✭✭

In UIWebView, the function ScalePageToFit can enable pinch zoom on the webview.
Because of UIWebView wll be deprecate, I change to WKWebView suggested by IOS, but ScalePageToFit is not there anymore.
Any idea how to make pinch zoom like UIWebView?

Best Answer

Answers

  • KhairulFLPMKhairulFLPM Member ✭✭

    @LandLu said:
    Try to add the javascript like:

    WKUserContentController userController = new WKUserContentController();
    var userScript = new WKUserScript(new NSString("var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"), WKUserScriptInjectionTime.AtDocumentEnd, true);
    userController.AddUserScript(userScript);
    WKWebViewConfiguration configuration = new WKWebViewConfiguration();
    configuration.UserContentController = userController;
    WKWebView wkWebView = new WKWebView(View.Bounds, configuration);
    wkWebView.LoadRequest(new NSUrlRequest(new NSUrl("https://www.microsoft.com")));    
    View.AddSubview(wkWebView);
    

    Hi @LandLu THANKS FOR QUICK RESPONSE
    But can I know where to put your code? Custom render?

  • LandLuLandLu Member, Xamarin Team Xamurai

    Yes, If you are using Xamarin.Forms.
    Reset the configurations there in the custom renderer of WKWebView.

  • KhairulFLPMKhairulFLPM Member ✭✭

    @LandLu I got error on View.Bounds and View.AddSubview
    Then I change 'View' to 'Control', my webpage appear but pinch zoom not working.
    I also included meta attribute 'initial-scale=1,maximum-scale=10,user-scalable=yes' , yet still nothing happen.
    Anything that I missed or need to add on, I'll appreciate your advice. Thanks :)

Sign In or Register to comment.