How to customize content in WebView in iOS?

I have UIWebView

. I am fetching some content from the server like: Image, Form, Audio, Video, etc. I want all the content to be web-friendly. No need to scroll through the web view. If the user scales the web view, it will scroll.

My code looks like this:

self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(12, 66, SCREEN_WIDTH - 24, SCREEN_HEIGHT - 126)];
self.webView.userInteractionEnabled = YES;
self.webView.backgroundColor = [UIColor clearColor];
self.webView.scalesPageToFit = TRUE;
self.webView.delegate = self;
self.webView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview: self.webView];

      

How can I fix this problem. I've already thought about it. But all the answers are in SO

self.webView.scalesPageToFit = TRUE;

      

But it doesn't work.

But I don't need it to scroll or scrollbar. I can hide the scrollbar, but I don't know how to auto-validate the webview.

+3


source to share


2 answers


The first option is to write the next tag to the HTML page.

<meta content="width=device-width; initial-scale=1.0; maximum-scale=1.5; user-scalable=YES;" name="viewport" />

      

second option



- (void)webViewDidFinishLoad:(UIWebView *)theWebView
    {
      CGSize contentSize = theWebView.scrollView.contentSize;
      CGSize viewSize = self.view.bounds.size;

      float zoomingSize = viewSize.width / contentSize.width;

      theWebView.scrollView.minimumZoomScale = zoomingSize;
      theWebView.scrollView.maximumZoomScale = zoomingSize;
      theWebView.scrollView.zoomScale = zoomingSize;  
    }

      

I hope this helps you

+1


source


You need to set the delegate from UIWebView and after your webview finishes loading, set the content width.

NSString *urlAddress = @"http://facebook.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[yourWebView loadRequest:requestObj];  
yourWebView.delegate = self;

      

Delegate UIWebView method

- (void)webViewDidFinishLoad:(UIWebView *)webview
{
  CGSize contentSize = theWebView.scrollView.contentSize;
  CGSize viewSize = self.view.bounds.size;

  float rw = viewSize.width / contentSize.width;

  webview.scrollView.minimumZoomScale = rw;
  webview.scrollView.maximumZoomScale = rw;
  webview.scrollView.zoomScale = rw;  
}

      



Or need to add the following code to the html head section

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.5; user-scalable=YES"/>

      

For your information

Doesn't work on IOS. The documentation says yes / no is used. I think it matters in this case. In obj-c, the values ​​are YES / NO and 1/0

+1


source







All Articles