Thursday, August 3, 2023

How NSHostingView Determines Its Sizing

Brian Webster:

I couldn’t get my SwiftUI view to expand to fill up the entire superview that the NSHostingView was being added to.

[…]

The next thing I came across was a property on NSHostingView called sizingOptions, which is described in the documentation as “The options for how the hosting view creates and updates constraints based on the size of its SwiftUI content.” Well that sounds promising! The default setting is all three options, [.minSize, .intrinsicContentSize, .maxSize], so I tried setting it to just [.minSize] and lo and behold, it worked! The Spacer was now growing to take up the whole height of the superview! (setting [.minSize, .maxSize] also worked)

But, there’s just one problem… this property was introduced in macOS 13 and I’m still targeting macOS 12. 😭 But, after seeing this property and how it works, I think I now understand what was going on with the size proposals earlier. I believe what NSHostingView does is to probe its rootView once each so it can set up constraints for a minimum size, intrinsic content size, and maximum size. […] So what I need to do is basically reimplement what the sizingOptions property is doing, which is to ignore the intrinsic content size of the SwiftUI view.

Previously:

Comments RSS · Twitter · Mastodon

Leave a Comment