我想要在我的应用程序中显示Web内容的预览。当用户单击Web视图预览时,它应该转到Web站点。
在我的代码下面
MainViewController.swift
import UIKit
import SDWebImage
import WebKit
class MainViewController: UIViewController, WebServiceDelegate, UITableViewDelegate, UITableViewDataSource, WKUIDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var indicator: UIActivityIndicatorView!
var tests: [Test] = []
let defaults = UserDefaults.standard
var webService = WebService()
var webView : WKWebView!
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
override func loadView() {
let webConfig = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfig)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://www.google.com/")
let myReq = URLRequest(url: myURL!)
webView.load(myReq)
label.center = tableView.center
label.textAlignment = .center
label.text = ""
tableView.isHidden = true
indicator.isHidden = true
indicator.center = view.center
webService.delegate = self
tableView.dataSource = self
tableView.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
HHTabBarView.shared.isHidden = false
navigationController?.navigationBar.isHidden = true
我在哪里可以使用内容预览?
if (defaults.object(forKey: "AccessToken") == nil) {
if let destinationView = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "IntroViewController") as? IntroViewController {
if let navigator = self.navigationController {
navigator.pushViewController(destinationView, animated: false)
}
}
} else {
startLoading(withView: view, withIndicator: indicator)
webService.getTests()
我有后端参数,如
webService.getWebContents(withWebImage: "webImage", withWebTitle: "webTitle", withWebDescription: "webDescription", withWebCreater: "webCreater", withWebPubDate: "webPubdate")
}
}
我想创建一个在我的预览中显示的Web视图
MainViewController表视图单元格。
func configureCell(for cell: UITableViewCell, with item: Test) {
let testImage = cell.viewWithTag(1001) as! UIImageView
testImage.sd_setImage(with: URL(string: item.image), completed: nil)
testImage.layer.borderWidth = 0
testImage.layer.borderColor = UIColor.black.cgColor
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return CGFloat(Utility.scaledHeight(320))
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tests.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "projectCell", for: indexPath)
cell.selectionStyle = .none
let item = tests[indexPath.row]
configureCell(for: cell, with: item)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let destinationView = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TestDetailViewController") as? TestDetailViewController {
if let navigator = self.navigationController {
navigator.pushViewController(destinationView, animated: true)
destinationView.testId = tests[indexPath.row].id
}
}
}
}
我在webView的顶部有一个导航视图。取消了这个小观点。添加了带有后箭头的正常UIButton
项目。 navigationController?.popToRootViewController(animated:true)