Post

Replies

Boosts

Views

Activity

Reply to WKWebView's video become black while paused in fullscreen state
This is working approach: import SwiftUI import WebKit struct YouTubeView: NSViewRepresentable { let webView: WKWebView let link: String init(link: String) { let configuration = WKWebViewConfiguration() configuration.preferences.isElementFullscreenEnabled = true let webView = WKWebView(frame: .zero, configuration: configuration) webView.translatesAutoresizingMaskIntoConstraints = true webView.autoresizingMask = [.width, .height] webView.allowsBackForwardNavigationGestures = false self.webView = webView self.link = link } func makeNSView(context: Context) -> NSView { return NSView() } func updateNSView(_ nsView: NSView, context: Context) { nsView.translatesAutoresizingMaskIntoConstraints = true nsView.addSubview(webView) guard let url = URL(string: link.embed) else { return } webView.load(.init(url: url)) } } // Build youtube link extension String { var embed: String { var strings = self.components(separatedBy: "/") let videoId = strings.last ?? "" strings.removeLast() let embedURL = strings.joined(separator: "/") + "/embed/\(videoId)" return embedURL } }
Jun ’23