Skip to content

Instantly share code, notes, and snippets.

@3d24rd0
Created April 12, 2019 10:04
Show Gist options
  • Save 3d24rd0/ae36189c3f9cd4b5f805725e404ec4a4 to your computer and use it in GitHub Desktop.
Save 3d24rd0/ae36189c3f9cd4b5f805725e404ec4a4 to your computer and use it in GitHub Desktop.
ios WebView Transparent background
class WebView {
let webView: WKWebView = {
let webView = WKWebView()
webView.translatesAutoresizingMaskIntoConstraints = false
webView.alpha = 1
webView.isOpaque = false
return webView
}()
override func viewDidLoad() {
self.view.backgroundColor = #colorLiteral(red: 0.180518508, green: 0.187897712, blue: 0.196665138, alpha: 1)
let myURL = URL(string:"http://www.yaminokishi.com")
let myRequest = URLRequest(url: url)
webView.load(myRequest)
view.addSubview(webView)
NSLayoutConstraint.activate([
webView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor, constant: -10),
webView.widthAnchor.constraint(equalTo: view.widthAnchor),
webView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -10),
])
}
func webViewDidFinishLoad(_ webView: WKWebView) {
webView.backgroundColor = UIColor.clear
webView.scrollView.backgroundColor = UIColor.clear
for subview in webView.subviews {
subview.backgroundColor = UIColor.clear
}
UIView.animate(withDuration: 0.5, animations: {
self.webView.alpha = 1
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment