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")!)
}
}
The behavior you're experiencing is most likely due to how the layout, constraints, and the interaction between the
WKWebViewand its superview are being managed. I've listed some way you can try to find the cause:Update Layout on Full Screen Exit: It could be that you may need to explicitly set the frame or constraints of the
WKWebViewwhen exiting full screen mode, to ensure it maintains its desired size. This can be done inviewWillTransition(to:with:)orviewDidLayoutSubviews().Check Constraints: Check that the
WKWebViewhas 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.