看起来确实没有创建工具提示的本地方法。
但是这里有一个解决方案:
import Foundation
import SwiftUI
public extension View {
/// Overlays this view with a view that provides a Help Tag.
func toolTip(_ toolTip: String) -> some View {
self.overlay(TooltipView(toolTip))
}
}
private struct TooltipView: NSViewRepresentable {
let toolTip: String
init(_ toolTip: String?) {
if let toolTip = toolTip {
self.toolTip = toolTip
}
else
{
self.toolTip = ""
}
}
func makeNSView(context: NSViewRepresentableContext<TooltipView>) -> NSView {
NSView()
}
func updateNSView(_ nsView: NSView, context: NSViewRepresentableContext<TooltipView>) {
nsView.toolTip = self.toolTip
}
}
已接受的解决方案不再编译。这对我有用:
public extension View {
/// Overlays this view with a view that provides a Help Tag.
func toolTip(_ toolTip: String) -> some View {
self.overlay(TooltipView(toolTip: toolTip))
}
}
private struct TooltipView: NSViewRepresentable {
let toolTip: String
func makeNSView(context: NSViewRepresentableContext<TooltipView>) -> NSView {
NSView()
}
func updateNSView(_ nsView: NSView, context: NSViewRepresentableContext<TooltipView>) {
nsView.toolTip = self.toolTip
}
}