WKWebView Mac Browser Enable fullscreen html5?

前端 未结 2 1541
南笙
南笙 2020-12-19 16:35

I made a simple browser with Swift last version and xcode 8.3.3. I want to be able to enter in fullscreen when there is an html5 video (like on youtube). I get \"full screen

2条回答
  •  既然无缘
    2020-12-19 16:36

    To allow WKWebView to use fullscreen HTML you need to access private API's (see https://github.com/WebKit/webkit/blob/master/Source/WebKit/UIProcess/API/Cocoa/WKPreferences.mm#L232-L240) in WKPreferences. Since you're using Swift in your bridging header add:

    #import 
    
    @interface WKPreferences ()
    -(void)_setFullScreenEnabled:(BOOL)fullScreenEnabled;
    @end
    
    

    And simply call it:

    let webView = WKWebView(frame: view.frame)
    webView.configuration.preferences._setFullScreenEnabled(true)
    

    If you notice strange resizing of the web view once you exit fullscreen I found that this fixes the issue for me:

    webView.autoresizingMask = [.width, .height]
    view.addSubview(webView)
    
    webView.translatesAutoresizingMaskIntoConstraints = false
    webView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    webView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    webView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    webView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    

    Note: Since you're accessing private API this would be rejected on the Mac App Store.

提交回复
热议问题