我试图在单击按钮时创建WKWebView。
我所有的视图都是通过代码创建的,而不是通过Storyboard或Xibs
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func pressed(_ sender: Any) {
let myBlog = "https://apple.com/"
let url = URL(string: myBlog)
let request = URLRequest(url: url!)
webView = WKWebView(frame: self.view.frame)
webView.navigationDelegate = self
webView.load(request)
self.view.addSubview(webView)
self.view.sendSubview(toBack: webView)
}
/// More code to show elements on the view
发生IBAction事件时,确实出现了Apple网站,但视图内容位于背景中,带有sendSubView toBack。
是否有更好的方法在单击WKWebView时在不单击导航到新视图的情况下打开URL?