I am using an NSOutlineView via NSViewRepresentable in a SwiftUI application running on macOS. Everything has been working fine.
Up until lately, I've been returning a custom NSView for each item using the standard:
func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
  // View recycling omitted.
  return MyItemView(item)
}
Now I want to explore using a little bit more SwiftUI and returning an NSHostingView from this delegate method.
func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
  // View recycling omitted.
  let rootView = MySwiftUIView(item)
  let hostingView = NSHostingView(rootView: rootView)
  return hostingView
}
For the most part, this appears to be working fine. NSOutlineView is even correctly applying highlight styling, so that's great.
But there's one small glitch. The outline view's disclosure triangles do not align with the hosting view's content. The disclosure triangles appear to just be pinned to the top. Perhaps they can't find a baseline constraint or something?
Is there any SwiftUI modifier or AppKit/SwiftUI technique I can apply here to get the disclosure button to appear in the right place?
Here is what the SwiftUI + NSHostingView version looks
like:
Note the offset disclosure indicators. (Image spacing is a bit off as well using Label, but fixable.
Here is what an NSView with NSTextFields looks like:
Disclosure indicators are correctly aligned, as you would expect.