visionOS WKWebView FullScreen bug

202 views Asked by At

I am having a problem when trying to implement a WKWebView in a window in VisionOS and that when trying to do it in full screen and exiting full screen, the size of the webview changes, becoming smaller or larger while the window remains the same size as before.

I used webView.configuration.preferences.isElementFullscreenEnabled = true for enabling fullscreen mode. I made a simple code to test this.

import SwiftUI

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup()  {
            ContentView()
        }
    }
}
import SwiftUI
import WebKit

struct WebView: UIViewRepresentable {
    let url: URL
    
    func makeUIView(context: Context) -> WKWebView  {
        let wkwebView = WKWebView()
        wkwebView.configuration.preferences.isElementFullscreenEnabled = true
        let request = URLRequest(url: url)
        wkwebView.load(request)
        return wkwebView
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
    }
}

struct ContentView: View {
    var body: some View {
        WebView(url: URL(string: "https://glitch.com/~fullscreen-test")!)
    }
}
1

There are 1 answers

1
iOSaddict On

The behavior you're experiencing is most likely due to how the layout, constraints, and the interaction between the WKWebView and its superview are being managed. I've listed some way you can try to find the cause:

  1. Update Layout on Full Screen Exit: It could be that you may need to explicitly set the frame or constraints of the WKWebView when exiting full screen mode, to ensure it maintains its desired size. This can be done in viewWillTransition(to:with:) or viewDidLayoutSubviews().

  2. Check Constraints: Check that the WKWebView has the appropriate constraints set up to maintain its size relative to its superview. The webview's size may change unexpectedly when transitioning if the constraints are not set up correctly. You can do this by using the view hierarchy in Xcode's debugging tools. Check before and after transitioning to full screen mode to see if there are any differences.