如何以编程方式在 TableView 上添加 WKWebView
How add WKWebView on TableView programmatically
我正在尝试制作一个简单的应用程序,将数据从网络映射到 table。但是站点需要OAuth 2.0的授权和token的接收。想要以编程方式为此显示带有 WKWebView 的视图。我这样做:
import UIKit
import WebKit
class TableViewController: UITableViewController, WKUIDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: CGRect(x: 0.0, y: 0.0, width: tableView.bounds.width, height: tableView.bounds.height), configuration: webConfiguration)
webView.uiDelegate = self
tableView.addSubview(webView)
let myRequest = URLRequest(url: URL(string: "https://www.google.ru/")!)
webView.load(myRequest)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "idFriendCell", for: indexPath)
cell.textLabel?.text = String(indexPath.row)
return cell
}
}
但是,分隔符 TableView 与我的 WebView 重叠:
怎么做才对?
至少你在这里有 2 个简单的解决方案。
1) 您可以创建自定义 UIViewController
,替换您的 TableViewController
。即继承 UIViewController
而不是 UITableViewController
。在此处手动添加 tableView
,如果需要,将 controller
与 UITableViewDataSource
和 UITableViewDelegate
一致。然后将 webView
添加到其 view
.
2) 您可以新建controller
,添加webView
。然后 present new controller
在你的 TableViewController
我正在尝试制作一个简单的应用程序,将数据从网络映射到 table。但是站点需要OAuth 2.0的授权和token的接收。想要以编程方式为此显示带有 WKWebView 的视图。我这样做:
import UIKit
import WebKit
class TableViewController: UITableViewController, WKUIDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: CGRect(x: 0.0, y: 0.0, width: tableView.bounds.width, height: tableView.bounds.height), configuration: webConfiguration)
webView.uiDelegate = self
tableView.addSubview(webView)
let myRequest = URLRequest(url: URL(string: "https://www.google.ru/")!)
webView.load(myRequest)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "idFriendCell", for: indexPath)
cell.textLabel?.text = String(indexPath.row)
return cell
}
}
但是,分隔符 TableView 与我的 WebView 重叠:
至少你在这里有 2 个简单的解决方案。
1) 您可以创建自定义 UIViewController
,替换您的 TableViewController
。即继承 UIViewController
而不是 UITableViewController
。在此处手动添加 tableView
,如果需要,将 controller
与 UITableViewDataSource
和 UITableViewDelegate
一致。然后将 webView
添加到其 view
.
2) 您可以新建controller
,添加webView
。然后 present new controller
在你的 TableViewController