How to determine the content size of a WKWebView?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Determining the content size of a WKWebView is mostly a timing problem. If you read the size too early, you will get incomplete values; if you wait until navigation finishes and the layout settles, you can usually use either the embedded scroll view's content size or JavaScript that asks the document for its rendered height.
The Simplest Native Check
WKWebView contains an internal UIScrollView, and its contentSize is often the first thing developers look at.
This works well when the page has fully loaded and laid out by the time didFinish runs.
When contentSize Is Not Enough
Some pages change after initial load because of:
- injected JavaScript
- dynamic images
- delayed fonts
- client-side rendering
In those cases, didFinish may still be too early to trust the final height. A JavaScript query can be more explicit about the document's own layout.
Reading the Height With JavaScript
You can evaluate JavaScript after navigation completes:
This is often the most direct way to size the web view according to the rendered HTML document.
Adjusting a Height Constraint
If the web view is embedded in another scrolling layout, a common pattern is to update a height constraint once the content size is known.
This is a standard way to make a non-scrolling WKWebView expand to fit its content.
Observe Changes for Dynamic Content
If the page content keeps changing, you may need to observe the scroll view's contentSize instead of measuring only once.
That approach is useful when:
- the page loads images later
- scripts inject additional HTML
- the final content height depends on async activity
The important point is that content size is not always fixed at initial page load.
Width Affects Height
A web page's height depends on its rendering width. If the web view width changes because of device rotation or Auto Layout updates, the content height can change too.
So if you cache the size too early or reuse it after the width changes, the result may be wrong. Always measure in the layout state you actually care about.
Common Pitfalls
The biggest mistake is reading scrollView.contentSize before the page has finished loading and laid out.
Another issue is assuming didFinish always means "final visual size is settled." Dynamic pages can continue changing after navigation finishes.
Developers also forget that width changes affect rendered height. A height measured in portrait may be wrong in landscape.
Finally, if you are embedding the web view inside another scroll view, be clear about which layer should scroll. Mixing scrolling responsibilities often causes layout confusion.
Summary
- '
WKWebViewcontent size is usually read after navigation finishes, not immediately after loading starts.' - '
webView.scrollView.contentSizeis a useful native measurement for many pages.' - '
evaluateJavaScript("document.documentElement.scrollHeight")is often the clearest way to get rendered document height.' - Update height constraints after measurement if the web view should expand with its content.
- Re-measure when content changes dynamically or the web view width changes.

