How to determine the content size of a UIWebView?
It turned out that my first guess using -sizeThatFits:
was not completely wrong. It seems to work, but only if the frame of the webView is set to a minimal size prior to sending -sizeThatFits:
. After that we can correct the wrong frame size by the fitting size. This sounds terrible but it's actually not that bad. Since we do both frame changes right after each other, the view isn't updated and doesn't flicker.
Of course, we have to wait until the content has been loaded, so we put the code into the -webViewDidFinishLoad:
delegate method.
Obj-C
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
NSLog(@"size: %f, %f", fittingSize.width, fittingSize.height);
}
Swift 4.x
func webViewDidFinishLoad(_ webView: UIWebView) {
var frame = webView.frame
frame.size.height = 1
webView.frame = frame
let fittingSize = webView.sizeThatFits(CGSize.init(width: 0, height: 0))
frame.size = fittingSize
webView.frame = frame
}
I should point out there's another approach (thanks @GregInYEG) using JavaScript. Not sure which solution performs better.
Of two hacky solutions I like this one better.
UIWebView : Get Webview Content size?
When font change at that time change Webview
height to 1 than get proper offsetHeight
for Webview
webView1.frame.size.height = 1
let size : String = self.webView1.stringByEvaluatingJavaScript(from: "document.documentElement.offsetHeight")!
how to get proper UIWebView content height?
Adopt this method:
- (void) webViewDidFinishLoad:(UIWebView *)webView{
CGRect frame = wvContent.frame;
CGSize fittingSize = [wvContent sizeThatFits:CGSizeZero];
frame.size = fittingSize;
wvContent.frame = frame;
}
Then call this method everytime device rotating:
- (void) reloadContent
{
CGRect frame = wvContent.frame;
frame.size.height = 1;
frame.size.width = wvContent.frame.size.width; // you should change webview's width here.
wvContent.frame = frame;
[wvContent loadHTMLString:yourHTML baseURL:nil]; // reload webview
}
Hope that help :)
UIWebView dynamic content size
This post has been updated for Swift 5 & WKWebView
So this is a really great function you wrote there, OP!
Here is just a shorter, more elegant version of your code:
// make sure to declare the delegate when creating your webView (add UIWebViewDelegate to class declaration as well)
myWebView.delegate = self
func webViewDidFinishLoad(webView: UIWebView) {
webView.frame.size.height = 1
webView.frame.size = webView.sizeThatFits(CGSize.zero)
}
Migrating to WKWebView
1) import WebKit
2) make your ViewController
inherit from WKNavigationDelegate
3) hook up the WKWebView
’s delegate: webView.navigationDelegate = self
4) implement the following protocol function:
webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
After migrating from UIWebView
to WKWebView
, above approach doesn’t seem to work anymore.
What you can do instead, is change the line with webView.sizeThatFits(CGSize.zero)
to:
webView.frame.size = webView.scrollView.contentSize
The full code for WKWebView
would then be:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.frame.size.height = 1
webView.frame.size = webView.scrollView.contentSize
}
Related Topics
Collectionview with the Horizontal Scroll with Mulitple Section
Sqlite Database on Phonegap/iOS - More Than 5Mb Possible
What's Currently the "Correct" Way to Set a Uiview's Corner Radius
How to Programmatically Dismiss Uialertcontroller Without Any Buttons
Apple MACh-O Linker & Ditto Error - Xcode 8
Change the Listing Order of the View Controllers in Xcode Storyboard
How to Keep the Keyboard from Covering My UI Instead of Resizing It
Passing Data with Segue Through Navigationcontroller
Uitableviewcell Selected Background Color on Multiple Selection
Swift - Must Call a Designated Initializer of the Superclass Skspritenode Error
Skspritenode Position in Universal Game
How to Efficiently Find Cgrects for Visible Words in Uitextview
Push/Pop View Controller with Navigation Bar from View Controller Without Navigation Bar
How to Use Uiactivityitemprovider to Send an Email with Attachment with Uiactivityviewcontroller
Cannot Run Application on Simulator After Installing Xcode 11 - Cfbundleversion Error