Skip to content

Instantly share code, notes, and snippets.

@catalinaturlea
Last active April 17, 2025 18:30
Show Gist options
  • Save catalinaturlea/cd1722c2d98dca5f92d57a3a7f0dc9b0 to your computer and use it in GitHub Desktop.
Save catalinaturlea/cd1722c2d98dca5f92d57a3a7f0dc9b0 to your computer and use it in GitHub Desktop.
Adding a WKWebView programmatically - together with the constraints
private weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// This is needed when using an iOS target < iOS 11.0 - there was a storyboard bug which does not allow instantiating WKWebView from the Storyboard or Xib file
let webView = WKWebView()
self.webView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(webView)
self.webView = webView
// Add the constraints to fill in the whole view
NSLayoutConstraint.activate([
self.webView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
self.webView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
self.webView.topAnchor.constraint(equalTo: self.view.topAnchor),
self.webView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment