Here is what I'm trying to do
I have a webView whose frame I set as
self.webView.frame = CGRectMake(0, Y,screenWidth,1);
y coordinate is calculated based on the height of other views that are added dynamically, which works great.
The screen width is set to 320 or 480 depending on the orientation of the device, which also works great.
Now I want my webView to set its width as the width of the screen and change it only in height to fit the content.
The content may 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 the HTML that I load as
[self.webView loadHTMLString:htmlMsgBody baseURL:nil]
In my WebViewDidFinishLoad method, I do 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;
My scrollView on which webView is installed can scroll horizontally. Anyway, my web browser is scrollLocked. Therefore, I should always keep the width of the web browser the same as self.view.frame.size.width, based on the orientation of the device, and the width of the webView should only change to match the content. So I set scrollView contentSize to enable vertical scrolling.
But the fact is that the self.webView method sizeThatFits returns strange values, and what happens is that when my screen width is 320, I get fitSize.width as 408, and my content turns off horizontally, and I canβt show correctly. Even if I forced the frame self.webView.frame.size.width = screenWidth, some of the content remains off the screen.
How to cope with this situation?