I have a complex app that requires the main SwiftUI view of the app to be embedded inside an NSHostingView which is a subview of an NSViewController's view. Then this NSViewController is wrapped using NSViewControllerRepresentable to be presented using SwiftUI's Window. And if I have a TimelineView inside my SwiftUI view hierarchy, it causes constant recalculation of the layout.
Here's a simplified demo code:
@main
struct DogApp: App {
private let dogViewController = DogViewController()
var body: some Scene {
Window("Dog", id: "main") {
DogViewControllerUI()
}
}
}
private struct DogViewControllerUI: NSViewControllerRepresentable {
let dogViewController = DogViewController ()
func makeNSViewController(context: Context) -> NSViewController { dogViewController }
func updateNSViewController(_ nsViewController: NSViewController, context: Context) {}
func sizeThatFits(_ proposal: ProposedViewSize, nsViewController: NSViewController, context: Context) -> CGSize? {
debugPrint("sizeThatFits", proposal)
return nil
}
}
public class DogViewController: NSViewController {
public override func viewDidLoad() {
super.viewDidLoad()
let mainView = MainView()
let hostingView = NSHostingView(rootView: mainView)
view.addSubview(hostingView)
hostingView.translatesAutoresizingMaskIntoConstraints = false
hostingView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
hostingView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
hostingView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
hostingView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
}
struct MainView: View {
var body: some View {
VStack {
TimelineView(.animation) { _ in
Color.random
.frame(width: 100, height: 100)
}
}
}
}
extension Color {
static var random: Color {
Color(
red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1)
)
}
}
When running it's printing out this repeatedly (multiple times a second).
"sizeThatFits" SwiftUI.ProposedViewSize(width: Optional(559.0), height: Optional(528.0))
"sizeThatFits" SwiftUI.ProposedViewSize(width: Optional(0.0), height: Optional(0.0))
"sizeThatFits" SwiftUI.ProposedViewSize(width: Optional(559.0), height: Optional(528.0))
"sizeThatFits" SwiftUI.ProposedViewSize(width: Optional(0.0), height: Optional(0.0))
"sizeThatFits" SwiftUI.ProposedViewSize(width: Optional(559.0), height: Optional(528.0))
"sizeThatFits" SwiftUI.ProposedViewSize(width: Optional(0.0), height: Optional(0.0))
"sizeThatFits" SwiftUI.ProposedViewSize(width: Optional(559.0), height: Optional(528.0))
If I run an equivalent code for an iPad, it only prints twice. If I comment out TimelineView on macOS, then it only prints out the above logs when resizing the app window.
The main reason this is an issue is that it's clearly causing dramatic degradation in performance. I was told to submit a bug report after I submitted TSI so a SwiftUI engineer could investigate it. Case-ID: 7461887. FB13810482. This was back in May but I received no response. LLMs are no help, and I've experimented with all sorts of workarounds. My last hope is this forum, maybe someone has an idea of what might be going on and why the recalculation is happening constantly on macOS.