iPhone - Auto resizing UIWebView content do not fit the frame

后端 未结 8 761
太阳男子
太阳男子 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 17:38

    You can do the following:

    oneview.contentMode = UIViewContentModeScaleAspectFit;
    

    That will make the content of your view grow as your view grows. The content will grow as big as possible while still fitting within the UIWebView and without distortion.

    There are other options for this property, and you will find a pretty good explanation of the more complicated ones, along with pictures showing the effect of each, in the View Programming Guide for iOS

    Note that the autoresizing mask you set will make the view itself grow only when its superview grows. If self.view is already big when the small UIWebView is created and self.view does not grow, the UIWebView won't be growing. You're probably aware of this, but I'm adding it just in case, since we can't see self.view's frame in this snippet of code.

    Hope this is helpful.

    0 讨论(0)
  • 2020-12-23 17:45

    For Swift 2.2 I have achieved the same with

    //Document file url
    var docUrl = NSURL(string: "https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0ahUKEwjjwPSnoKfNAhXFRo8KHf6ACGYQFggbMAA&url=http%3A%2F%2Fwww.snee.com%2Fxml%2Fxslt%2Fsample.doc&usg=AFQjCNGG4FxPqcT8RXiIRHcLTu0yYDErdQ&sig2=ejeAlBgIZG5B6W-tS1VrQA&bvm=bv.124272578,d.c2I&cad=rja")
    
    let req = NSURLRequest(URL: docUrl!)
    webView.delegate = self
    //here is the sole part
    webView.scalesPageToFit = true
    webView.contentMode = .ScaleAspectFit
    webView.loadRequest(req)
    
    0 讨论(0)
  • 2020-12-23 17:45

    Try this:

    [oneView setScalesPageToFit:YES];
    

    ... instead of:

    oneView.scalesPageToFit = YES;
    

    Hope that helps.

    0 讨论(0)
  • 2020-12-23 17:46

    The answer is: you are already doing this but there are limits.

    Look at the following screenshots:

    Screeshot 1, scalesPageToFit YES and NO

    Both webview have the same width. In the lower one the page fits perfectly.

    enter image description here

    Screeshot 2, scalesPageToFit both YES but smaller widths set

    Both webview try to fit the page but it won't as there is a size limit.

    enter image description here

    0 讨论(0)
  • 2020-12-23 17:51

    Use sizethatfits() in the webview which would resolve your issue.

    0 讨论(0)
  • 2020-12-23 17:53

    Try this.That's working for me.

    NSString *strTemplateHTML = [NSString stringWithFormat:@"<html><head><style>img{max-width:100%%;height:auto !important;width:auto !important;};</style></head><body style='margin:0; padding:0;'>%@</body></html>", @"insert your html content here"];
    
    [webView loadHTMLString:strTemplateHTML baseURL:nil];
    
    0 讨论(0)
提交回复
热议问题