iOS WkWebViewRenderer width

EJ 366 Reputation points
2021-04-30T07:49:42.497+00:00

Hi,

In old UIWebView you could set ScalesPageToFit, but new WkWebViewRenderer doesn't have this property. I want to make WebView width to be same size as device width, so no horizontal scrolling.

No matter what I've tried (injecting "meta name='viewport' content='width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no'" into html) and so on, if content width is greater than device it allows to horizontally scroll.

Any ideas how can I fix this? Thanks in advance!

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2021-04-30T08:27:33.03+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    In old UIWebView you could set ScalesPageToFit, but new WkWebViewRenderer doesn't have this property.

    Try using the following code to make the webView to fit the screen in the custom WkWebViewRenderer class.

       public class CustomWebViewRenderer : WkWebViewRenderer  
       {  
           const string JavaScriptFunction = ...;  
           WKUserContentController userController;  
         
           public CustomWebViewRenderer() : this(new WKWebViewConfiguration())  
           {  
           }  
         
           public CustomWebViewRenderer(WKWebViewConfiguration config) : base(config)  
           {  
               userController = config.UserContentController;  
               var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false);  
               userController.AddUserScript(script);  
               config.UserContentController = userController;  
               WKWebView webView = new WKWebView(Frame, config);  
           }  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.