In using SwiftUI, the WKWebView loads successfully when implemented within a UIViewRepresentable wrapper. However, the following logs appear in the Debug area of Xcode Version 13.4.1:
[ViewportSizing] maximumViewportInset cannot be larger than frame
[ViewportSizing] minimumViewportInset cannot be larger than frame
How can these logs be resolved?
These logs can be reproduced with the following minimal reproducible example:
WebApp.swift
import SwiftUI
import WebKit
@main
struct WebApp: App {
var body: some Scene {
WindowGroup {
WebView()
}
}
}
struct WebView: UIViewRepresentable {
func makeUIView(context: Context) -> some UIView {
let webView = WKWebView()
webView.load(URLRequest(url: URL(string: "https://www.google.com")!))
return webView
}
func updateUIView(_ uiView: UIViewType, context: Context) {}
}
PROGRESS:
These logs seem to originate from the following WebKit browser engine source code:
- maximumViewportInset: https://github.com/WebKit/webkit/blob/main/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm#L1623
- minimumViewportInset: https://github.com/WebKit/webkit/blob/main/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm#L1638
These logs persist despite commenting out the webView.load(URLRequest(url: URL(string: "https://www.google.com")!))
line. So, it's concludable that it's not a website issue.
CodePudding user response:
Answer
This can be resolved by setting the frame to more than zero by changing this:
let webView = WKWebView()
To this:
let webView = WKWebView(frame: CGRect(x: 0.0, y: 0.0, width: 0.1, height: 0.1))
Explanation
In looking at the WKWebView source code, it appears that the logs are due to the following logic:
- Logic that logs
[ViewportSizing] maximumViewportInset cannot be larger than frame
auto maximumViewportInsetSize = WebCore::FloatSize(maximumViewportInset.left maximumViewportInset.right, maximumViewportInset.top additionalTopInset maximumViewportInset.bottom);
auto minimumUnobscuredSize = frame - maximumViewportInsetSize;
if (minimumUnobscuredSize.isEmpty()) {
if (!maximumViewportInsetSize.isEmpty()) {
...
RELEASE_LOG_ERROR(ViewportSizing, "maximumViewportInset cannot be larger than frame");
}
...
}
- Logic that logs
[ViewportSizing] minimumViewportInset cannot be larger than frame
auto minimumViewportInsetSize = WebCore::FloatSize(minimumViewportInset.left minimumViewportInset.right, minimumViewportInset.top additionalTopInset minimumViewportInset.bottom);
auto maximumUnobscuredSize = frame - minimumViewportInsetSize;
if (maximumUnobscuredSize.isEmpty()) {
if (!minimumViewportInsetSize.isEmpty()) {
...
RELEASE_LOG_ERROR(ViewportSizing, "minimumViewportInset cannot be larger than frame");
}
...
}
We can inspect these values by adding the following codes in makeUIView
:
print("maximumViewportInset: \(webView.maximumViewportInset)")
print("minimumViewportInset: \(webView.minimumViewportInset)")
print("frame: \(webView.frame)")
The outputs are as follows:
maximumViewportInset: UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
minimumViewportInset: UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
frame: (0.0, 0.0, 0.0, 0.0)
As we can observe, maximumViewportInset
, minimumViewportInset
, and frame
all have values of zeros upon WKWebView()
initialization. Hence, the logs are triggered.