UIWebView
Content Size
iOS Development
Mobile App
Web Development

How to determine the content size of a UIWebView?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In legacy iOS code, UIWebView content size is usually needed when the web view sits inside another scrollable layout or must resize to fit HTML content. The key detail is timing: the content size is not final until the page finishes loading, and the most reliable value often comes either from the embedded scroll view or from JavaScript executed after render.

Read the Size After the Page Finishes Loading

UIWebView populates its internal scroll view as content is laid out. If you query size too early, you will often get zero or a partial height. The usual place to inspect it is webViewDidFinishLoad.

swift
1import UIKit
2
3final class ViewController: UIViewController, UIWebViewDelegate {
4    let webView = UIWebView()
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8        webView.delegate = self
9        webView.frame = view.bounds
10        view.addSubview(webView)
11
12        let html = "<html><body><h1>Hello</h1><p>Longer content here</p></body></html>"
13        webView.loadHTMLString(html, baseURL: nil)
14    }
15
16    func webViewDidFinishLoad(_ webView: UIWebView) {
17        let size = webView.scrollView.contentSize
18        print("content size: \(size)")
19    }
20}

For many cases, webView.scrollView.contentSize is enough. It reflects the scrollable area after layout.

Use JavaScript When You Need Document Measurements

Sometimes the scroll view size is close enough, but sometimes you need the rendered document height itself. In that case, ask the page for document.body.scrollHeight or a similar property.

swift
1func webViewDidFinishLoad(_ webView: UIWebView) {
2    let heightString = webView.stringByEvaluatingJavaScript(
3        from: "document.body.scrollHeight"
4    ) ?? "0"
5
6    let widthString = webView.stringByEvaluatingJavaScript(
7        from: "document.body.scrollWidth"
8    ) ?? "0"
9
10    let height = Double(heightString) ?? 0
11    let width = Double(widthString) ?? 0
12
13    print("document size: \(width) x \(height)")
14}

This approach is useful when CSS layout affects the final rendered document in ways that are not obvious from the container alone.

Resize the Web View to Fit Its Content

A common use case is embedding the web view in a larger static layout and removing its own scrolling. In that case, disable scrolling and set the frame height after load.

swift
1func webViewDidFinishLoad(_ webView: UIWebView) {
2    webView.scrollView.isScrollEnabled = false
3
4    let heightString = webView.stringByEvaluatingJavaScript(
5        from: "document.body.scrollHeight"
6    ) ?? "0"
7
8    let height = CGFloat(Double(heightString) ?? 0)
9    var frame = webView.frame
10    frame.size.height = height
11    webView.frame = frame
12}

If the HTML loads images asynchronously, the first height may still be too small. In that case, trigger another measurement after the images finish loading or after a short deferred layout pass.

Prefer WKWebView in New Code

UIWebView is a deprecated API. If you are maintaining legacy code, the techniques above are still useful, but new work should move to WKWebView. The same general idea applies there: wait until rendering is complete, then read scrollView.contentSize or evaluate JavaScript.

A modernized migration path often removes sizing bugs because WKWebView has a clearer process model and better JavaScript integration.

Common Pitfalls

The most common mistake is reading contentSize before the page has finished loading. Another common issue is measuring before remote images, fonts, or injected scripts have completed, which leads to heights that change after the first layout pass.

Be careful when nesting a UIWebView inside another UIScrollView. Competing scroll views can produce awkward gesture behavior and misleading assumptions about which component should own vertical sizing. Also remember that JavaScript measurements return strings, so they must be parsed before use.

Summary

  • measure UIWebView content after webViewDidFinishLoad
  • use webView.scrollView.contentSize for the simplest native size check
  • use JavaScript such as document.body.scrollHeight when you need document-level measurements
  • resize the view only after content has actually rendered
  • prefer WKWebView for new code, but maintain legacy UIWebView sizing through its scroll view or JavaScript hooks

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.