我已将webView添加到我的应用中,可通过按一个按钮将其打开。 在此webView上方,我添加了一个按钮,该按钮应关闭webView。
如果我按下按钮,WebView应该像它应该那样关闭,但是它只显示黑屏,我想我必须重新加载ViewController。 我该怎么办?
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
let button = ButtonDesign(frame: CGRect(x: 320, y: 50, width: 50, height: 50))
button.setTitle("X", for: .normal)
button.addTarget(webView, action: #selector(webView.removeFromSuperview), for: UIControl.Event.touchUpInside)
self.view.addSubview(button)
答案 0 :(得分:0)
当前,您将webview设置为vc的视图,但是需要使用addSubview
let webConfiguration = WKWebViewConfiguration()
// set a non-zero frame
webView = WKWebView(frame:view.frame, configuration: webConfiguration)
webView.uiDelegate = self
view.addSubview(webView) // add it here
view.backgroundColor = .red // change view's color
let button = ButtonDesign(frame: CGRect(x: 320, y: 50, width: 50, height: 50))
button.setTitle("X", for: .normal)
button.addTarget(webView, action: #selector(webView.removeFromSuperview), for: UIControl.Event.touchUpInside)
self.view.addSubview(button)
删除时
webView.removeFromSuperview()
答案 1 :(得分:0)
您需要将webview
添加为subview
。
您实际上是将self.view
替换为Web视图。
view = webView <--- ITS' WRONG: viewcontroller's view is replaced by web view
将上面的代码行替换为以下内容...
view.addSubview(webView)
最终代码应类似于...
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view.addSubview(webView)
答案 2 :(得分:0)
如果要向后导航,则应关闭或弹出ViewController。
self.dismiss(animated: true, completion: nil)
self.navigationController?.popViewController(animated: true)