UIWebView resize to fit Content

我只是一个虾纸丫 提交于 2019-12-11 02:20:20

问题


Here is what I am trying to do,

I have a webView whose frame I am setting as

self.webView.frame = CGRectMake(0, Y,screenWidth,1);

the y coordinate is calculated based on the height of the other views that are added dynamically which works fine.

the screenWidth is set to 320 or 480 depending on device orientation which also works fine.

Now I want my webView to set it's width as the screenWidth and resize it's height alone to fit the content.

The content could be plainText which I load as

        NSData* data=[msgBody dataUsingEncoding:NSUTF8StringEncoding];             
        if (data) 
        [self.webView loadData:data MIMEType:@"text/plain" textEncodingName:@"UTF-8" baseURL:nil];  

or HTML which I load as

        [self.webView loadHTMLString:htmlMsgBody baseURL:nil]; 

In my WebViewDidFinishLoad method, I am doing this,

- (void)webViewDidFinishLoad:(UIWebView *)webview {

    CGRect frame = self.webView.frame;
    CGSize fittingSize = [self.webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    self.webView.frame = frame;

   CGFloat totalHeight = self.webView.frame.origin.y+self.webView.frame.origin.height+30;

    //the webView is scrolllocked and put on a scrollView so I do this

    [self.theScrollView setContentSize:CGSizeMake(screenWidth, totalHeight)];
}

My scrollView on which the webView is put can scroll horizontally. My webView is scrollLocked anyways. So I have to always keep my webView width to be the same as self.view.frame.size.width based on the device orientation and the webView width should change just to fit the content. I accordingly set the scrollView contentSize to allow vertical scrolling.

But the thing is that, the self.webView sizeThatFits method returns weird values and what happens is that when my screenWidth is 320 I get fittingSize.width as 408 and my content goes off screen horizontally and I am not able to show the content properly. Even if I forcibly set the frame of self.webView.frame.size.width = screenWidth, part of the content remains off screen.

How to deal with this situation?


回答1:


According to the docs, size to fit:

Resizes and moves the receiver view so it just encloses its subviews.

This does not give you any information about fitting the webView into it's containing view. Is it possible that your webView is trying to display something wider than the screenWidth, and since you have it scroll locked, it is showing the whole width?




回答2:


I was also experiencing the problem of the UIWebView contents not sizing properly when switching from landscape to portrait.

After hunting around for awhile, I found my answer:

"One way I've found to handle this is to reload the content after the resize. When I say "reload," I don't mean the built-in reload function on UIWebView, I mean the actual call to loadHTMLString: baseURL: (or whichever method you use to load your content initially)." Agent Gold's post on Apple Support forum



来源:https://stackoverflow.com/questions/5914472/uiwebview-resize-to-fit-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!