0

我正在尝试在 WkWebView 中实现自定义 peek/pop 行为。我看到委托被调用并且控制器被返回,但是 peek 控制器没有出现。

我只能通过设置 self.webView?.userInteractionEnabled = false 来显示 peek 控制器

这里会发生什么?

代码:

class ViewController: UIViewController, WKNavigationDelegate, UIViewControllerPreviewingDelegate {

var webView: WKWebView?

func webView(webView: WKWebView,
    decidePolicyForNavigationResponse navigationResponse: WKNavigationResponse,
    decisionHandler: ((WKNavigationResponsePolicy) -> Void)){

        decisionHandler(.Allow)
}

override func viewDidLoad() {
    super.viewDidLoad()

    /* Create our preferences on how the web page should be loaded */
    let preferences = WKPreferences()
    preferences.javaScriptEnabled = true

    /* Create a configuration for our preferences */
    let configuration = WKWebViewConfiguration()
    configuration.preferences = preferences

    /* Now instantiate the web view */
    webView = WKWebView(frame: view.bounds, configuration: configuration)

    if let theWebView = webView{
        /* Load a web page into our web view */
        let url = NSURL(string: "http://www.apple.com")
        let urlRequest = NSURLRequest(URL: url!)
        theWebView.loadRequest(urlRequest)
        theWebView.navigationDelegate = self
        view.addSubview(theWebView)

    }

    self.registerForPreviewingWithDelegate(self, sourceView: self.view)
}

func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {

    self.webView?.userInteractionEnabled = false
    let peekViewController = storyboard!.instantiateViewControllerWithIdentifier("peekaboo") as UIViewController
    /*
    Set the height of the preview by setting the preferred content size of the detail view controller.
    Width should be zero, because it's not used in portrait.
    */
    peekViewController.preferredContentSize = CGSize(width: 0.0, height: 0.0)

    // Set the source rect to the cell frame, so surrounding elements are blurred.
    //previewingContext.sourceRect = (self.webkitWebView?.frame)!

    return peekViewController
}

/// Present the view controller for the "Pop" action.
func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
    // Reuse the "Peek" view controller for presentation.

}
}
4

1 回答 1

0

设置一个 preferedContentSize 高度

peekViewController.preferredContentSize = CGSize(width: 0.0, height: 150)
于 2016-01-04T09:44:29.673 回答