iPhone - Auto resizing UIWebView content do not fit the frame

后端 未结 8 762
太阳男子
太阳男子 2020-12-23 17:19

I\'m generating an UIWebView into my viewDidLoad method, with a tiny size (let\'s say something like 50x70). And then I put it into a super UIView.

I\'d like to ma

相关标签:
8条回答
  • 2020-12-23 18:02

    For those, who faced same problem, you can turn off the UIWebView native scrolling by

    self.webView.scrollView.scrollEnabled = NO;
    

    and add a separate scrollView which will handle scrolling and zooming instead of webView's native scroll. Next implement

    - (void) webViewDidFinishLoad:(UIWebView *)webView
    {
       CGRect frame = _webView.frame;
       CGSize fittingSize = [_webView sizeThatFits:_webView.scrollView.contentSize];
       frame.size = fittingSize;
       _webView.frame = frame;
       self.scrollView.contentSize = self.webView.scrollView.contentSize;
    }
    

    to set proper webView frame. Hope this helps someone. Good coding!

    0 讨论(0)
  • 2020-12-23 18:03

    For Swift 3, I have achieved the same with

    DispatchQueue.main.async {
                    if let finalURL = URL(string: urlString) {
                        let request = URLRequest(url: finalURL)
    
                        // self.webView.sizeToFit()
                        self.webView.scalesPageToFit = true
                        self.webView.contentMode = .scaleAspectFit
                        self.webView.loadRequest(request)
                    }
                }
    
    0 讨论(0)
提交回复
热议问题