您如何在悬停时显示工具提示/提示?

问题描述 投票:0回答:2

如何在某些视图上显示工具提示/提示?例如,在按钮上。

enter image description here

tooltip swiftui
2个回答
2
投票

看起来确实没有创建工具提示的本地方法。

但是这里有一个解决方案:

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
    }
}

0
投票

已接受的解决方案不再编译。这对我有用:

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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.