WKWebview调整在后台

时间:2017-07-31 01:44:55

标签: swift xcode uiwebview wkwebview

我已经切换了我的一个应用程序以使用WKWebview,这是第一次对我来说,我遇到了一个问题,因为你知道我无法从故事板控制它,我当您单击左上角的按钮时,在侧边栏中有一个滚动视图,但它看起来好像出现在视图的后面,我该如何解决这个问题呢?

        let configuration = WKWebViewConfiguration()
        configuration.preferences = preference

        webView = WKWebView(frame: view.bounds, configuration: configuration)

        view.addSubview(webView)

请注意我已点击该screeshot中的按钮,但滚动没有显示] 1

1 个答案:

答案 0 :(得分:0)

使用这个, 首先导入

import WebKit

设置WKWebview的委托

WKScriptMessageHandler , WKNavigationDelegate 

声明webview

var webView: WKWebView!
     var webConfiguration:WKWebViewConfiguration! = nil

    override func loadView() {
        webView = WKWebView(frame: .zero, configuration: self.webConfig())
        webView.navigationDelegate = self
        view = webView

    }

viewDidLoad中

override func viewDidLoad() {
        super.viewDidLoad()


        /*if let url = URL(string: "http://192.168.1.122/arvee/piyush/mobileViewPages/owner/manager.html") {
            let request = URLRequest(url: url)
            webView.load(request)
        }*/

        let url = Bundle.main.url(forResource: "AddAppointment/bookApp", withExtension:"html")
        let request = URLRequest(url: url!)
        webView.load(request)
    }

WKWebview代表

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        print(error)
    }


    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {

        let data = JSON(message.body)

    }


    func webConfig() -> WKWebViewConfiguration {
        //if webConfiguration != false {
            webConfiguration = WKWebViewConfiguration()
            let userController = WKUserContentController()
            userController.add(self, name: "buttonClicked")
            userController.add(self, name: "pageLoaded")
            webConfiguration.userContentController = userController
        //}

        return webConfiguration
    }