Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save YoomamaFTW/b17a645dba4d4d739e40f8a8a5cd06f6 to your computer and use it in GitHub Desktop.
Save YoomamaFTW/b17a645dba4d4d739e40f8a8a5cd06f6 to your computer and use it in GitHub Desktop.
Custom Table Header View with WKWebView not showing - Need Help
// THIS CODE DOES NOT SHOW THE HTML. The Gist is for an SO question and for future references.
import UIKit
import WebKit
class TableVC: UIViewController, WKNavigationDelegate {
let headerView = UITableViewHeaderFooterView()
let titleView = UILabel()
let webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
setupTable()
setupHeaderView()
tableView.tableHeaderView = headerView
}
func setupTable() {print("All of the table configurations are left out for the sake of cleanliness. They just included registering cells and using autolayout for the table.")}
func setupHeaderView() {
titleView.translatesAutoresizingMaskIntoConstraints = false
webView.translatesAutoresizingMaskIntoConstraints = false
webView.navigationDelegate = self
webView.loadHTMLString("<b>HELLO THERE.HELLO THERE.HELLO THERE. HELLO THERE.HELLO THERE.HELLO THERE.HELLO THERE.HELLO THERE.HELLO THERE.HELLO THERE.HELLO THERE.HELLO THERE.HELLO THERE.HELLO THERE.HELLO THERE.HELLO THERE.HELLO THERE.HELLO THERE.HELLO THERE.HELLO THERE.HELLO THERE.HELLO THERE.</b>",
baseURL: nil)
let lg = headerView.contentView
lg.addSubview(titleView)
lg.addSubview(webView)
let la = lg.leadingAnchor
let ta = lg.trailingAnchor
NSLayoutConstraint.activate([
titleView.topAnchor.constraint(equalTo: lg.topAnchor, constant: 4),
titleView.leadingAnchor.constraint(equalTo: la, constant: 4),
titleView.trailingAnchor.constraint(equalTo: ta, constant: -4),
titleView.bottomAnchor.constraint(equalTo: webView.topAnchor, constant: -7),
webView.leadingAnchor.constraint(equalTo: la, constant: 4),
webView.trailingAnchor.constraint(equalTo: ta, constant: -4),
webView.bottomAnchor.constraint(equalTo: lg.bottomAnchor, constant: -4)
])
}
@YoomamaFTW
Copy link
Author

I've been reading that I should use viewDidAppear or the navigation delegate that has didFinish, but 1. none of the html is actually loading or can be seen and 2. the WKWebView doesn't take up any space. Please help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment