iOS 版 swiftUI 中的自定义工具提示

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

我有一行颜色,我想为每种颜色显示一个工具提示。

struct ColorsView: View {
    let colors = [UIColor.red, UIColor.white, UIColor.gray, UIColor.blue, UIColor.black]
    var body: some View {
        HStack {
            ForEach(0..<colors.count) { index in
                Color(self.colors[index])
                .frame(width: (UIScreen.main.bounds.size.width - 30) / 5, height: 25)
            }
        }.cornerRadius(12)
    }
}

如何为此创建自定义工具提示?我尝试包装在 ZStack 中,但这似乎并不能完全解决问题。任何帮助:)

swiftui tooltip
2个回答
0
投票

您也可以尝试使用此工具:https://github.com/quassummanus/SwiftUI-Tooltip

这是一个如何通过简单的

Text
视图使用它的示例,您可以从那里进行推断。它非常好,因为它不依赖于任何 Apple 提供的不是 SwiftUI 基础的 API,因此您可以在所有平台上使用它。

Text("Say something nice...")
    .tooltip(.bottom) {
        Text("Something nice!")
    }

你会得到这样的结果:

example


0
投票

该解决方案不起作用

Text(“说点好听的话……”)

.tooltip(.bottom) {

    Text("Something nice!")
} 

还有其他解决办法吗

© www.soinside.com 2019 - 2024. All rights reserved.