Search code examples
iosswiftcgrect

In Swift, why does .zero fill the entire window in this example?


I'm following a tutorial on creating a WKWebView within a view controller and the WKWebView is instantiated with a CGRect value of .zero but the website opens to the entire size of the view controller when I run this code:

import Foundation
import UIKit
import WebKit

class WebViewController: UIViewController{

    var webView: WKWebView!

    override func loadView() {
        let myURL = URL.init(string: "https://www.apple.com")
        let webConfiguration = WKWebViewConfiguration()
        let myUrlRequest = URLRequest.init(url: myURL!)
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        view = webView
        webView.load(myUrlRequest)
    }
}

Doesn't .zero mean 0 for all values in CGRect?


Solution

  • The frame you provide is irrelevant. You are implementing loadView (not viewDidLoad). The web view webview you create in your code is the main view of this view controller. The frame of the main view of a view controller is never up to you; it is always resized by its parent view controller or, if it is the window's rootViewController, to fill the window.