After fullscreen the video in WkWebView, if pause it, the screen become black;
it happened in Xcode Version: 14.1, in my developing macOS App
Xcode 13.x no problem, until me upgrade to Xcode 14.1
After fullscreen the video in WkWebView, if pause it, the screen become black;
it happened in Xcode Version: 14.1, in my developing macOS App
Xcode 13.x no problem, until me upgrade to Xcode 14.1
I know why black, because I use
webView?.translatesAutoresizingMaskIntoConstraints = false
to avoid some wkwebview bug in Xcode 13.x;
now this code make black screen bug, remove it, black screen disappeared.
but
new bug appeared: after enter fullscreen again, the WebView become small size that same as main window.
headache~~
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
}
}