Resizing UIWebView UI

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; //the webView is scrolllocked and put on a scrollView so I do this [self.theScrollView setContentSize:CGSizeMake(screenWidth, totalHeight)]; } 

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?

+2
source share
2 answers

According to documents size by size:

Resizes and moves the view of the receiver, so it just closes the subviews .

This does not give you any information about installing webView in it containing the view. Is it possible that your webView is trying to display something wider than screenWidth, and since you have the scroll bar locked, it shows the entire width?

+3
source

I also had a problem with UIWebView content not matching the correct size when switching from landscape to portrait.

After hunting for a while, I found the answer:

"One of the ways I found for this is to reload the content after resizing. When I say reload, I don't mean the built-in reload function in UIWebView, I mean the actual call to load HTMLString: baseURL: (or which Whatever method you use to download your content initially.) Gold Agent post on Apple Support Forum

+1
source

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


All Articles