UIWebView does not load the response webpage correctly

I am trying to load a responsive webpage into a UIWebview using the following code in my viewDidLoad UIViewController method:

NSURL *url = [NSURL URLWithString:self.webViewURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];

Instead of loading the web page so that it looks based on the frame size of the UIWebView, it sets the UIWebView UIScrollView contentSize to some seemingly arbitrary size (larger than the size of my web view) and loads the responsive web page as he will look for this frame size.

If I try to download the URL that I use in Safari, it is ordered correctly according to the size of the browser window.

I tried setting the scalesPageToFit property to YES, although I really do not want to scale the page, but should it respond to the structure of the web view.

Does anyone have any ideas as to what is happening and how can I fix this?

+4
source share
5 answers

I understood what was going on. My UIWebView width on the iPad is less than the width of the device. The webpage used the width of the device to determine the width of the page (this code was in the element

):
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

I managed to fix the problem using what I found in another question ( fooobar.com/questions/1542673 / ... ):

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.querySelector('meta[name=viewport]').setAttribute('content', 'width=%d;', false); ", (int)webView.frame.size.width]];
}
+26
source

, UIWebView scalesPageToFit TRUE.

webView.scalesPageToFit = TRUE;

- UIWebView.

+7

- . - . - 0, -.

enter image description here

+1

UIWebView , UIWebView UIView NSAutoLayoutConstraint.

myWebView = UIWebView()
//Load WebPage
myView.addView(myWebView) // After web page is loaded
//Add constraints

UIWebView .

myWebView = UIWebView(frame: CGRect(x: 0, y: 0, width: myView.frame.width, height: myView.frame.height))

, .

0
source

For all of you Swifters, here's a super-clean extension using Darren's answer:

extension UIWebView {

    func updateLayoutForContent() {
        let javascript = "document.querySelector('meta[name=viewport]').setAttribute('content', 'width=\(self.bounds.size.width);', false); "
        self.stringByEvaluatingJavaScriptFromString(javascript)
    }

}

It’s convenient if you want to call it several times (for example, when the device rotates and the webView changes proportions).

0
source

Source: https://habr.com/ru/post/1542672/


All Articles