如何在从标签内容切换到文本时清除 SwiftUI Pickers 中的图标?

huangapple go评论55阅读模式
英文:

How do we clear the icon in SwiftUI Pickers when switching from Label content to Text?

问题

抱歉,我不能完成你的要求。

英文:

I'm curious, is a mix of Label and Text Views supported as the contents of a Picker? I'm seeing some visual glitches around the icon that suggests it is not.

In the example below, the picker initially appears correctly.

如何在从标签内容切换到文本时清除 SwiftUI Pickers 中的图标?

Selecting "First Item" works as expected.

如何在从标签内容切换到文本时清除 SwiftUI Pickers 中的图标?

But reverting to the original state, by selecting "None", results in a label that still retains a phantom icon from the previous selection.

如何在从标签内容切换到文本时清除 SwiftUI Pickers 中的图标?

Can anyone suggest a good workaround, if this is a bug? I tried using a label with an empty string for the systemImage (Label("None", systemImage: "").tag(0)). I also tried using a Label instead of the text, but without icon (Label("None", systemImage: "house").labelStyle(.titleOnly).tag(0)). In both cases the phantom icon was still there.

Here is the full code to illustrate the problem:

struct LabelAndTextInPicker: View {
    @State private var selection = 0
    var body: some View {
        Form {
            Picker("Choice", selection: $selection) {
                Text("None").tag(0)
                Label("First Item", systemImage: "1.circle").tag(1)
                Label("Second Item", systemImage: "2.circle").tag(2)
            }
            .pickerStyle(.menu)
        }
    }
}

答案1

得分: 1

这看起来像是一个错误。作为一种变通方法,尝试这样做,对我有效:

                Label {
                    Text("None")
                } icon: {
                    Image(uiImage: UIImage())
                }.tag(0)
英文:

It does look like a bug. As a workaround try this, works for me:

            Label {
                Text("None")
            } icon: {
                Image(uiImage: UIImage())
            }.tag(0)

huangapple
  • 本文由 发表于 2023年2月19日 07:41:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定