3 Replies
      Latest reply on Jan 16, 2020 9:27 AM by melgu
      mwp Level 1 Level 1 (10 points)

        How do I set the tooltip for an AppKit SwiftUI view?

        • Re: SwiftUI Tooltips
          mwp Level 1 Level 1 (10 points)

          For the record there appears to be no native way to set a tooltip on a SwiftUI view (feedback #FB7095924), but the following workaround seems to do the trick:

           

          public extension View {
              /// Overlays this view with a view that provides a toolTip with the given string.
              func toolTip(_ toolTip: String?) -> some View {
                  self.overlay(TooltipView(toolTip))
              }
          }
          
          private struct TooltipView: NSViewRepresentable {
              let toolTip: String?
          
              init(_ toolTip: String?) {
                  self.toolTip = toolTip
              }
          
              func makeNSView(context: NSViewRepresentableContext) -> NSView {
                  let view = NSView()
                  view.toolTip = self.toolTip
                  return view
              }
          
              func updateNSView(_ nsView: NSView, context: NSViewRepresentableContext) {
              }
          }