菜单标题和降低的不透明度问题

4
我想使用 SwiftUI 的新 Menu,但不幸的是,我无法解决一个问题。在选择 Menu 的一个值后,菜单标题看起来像这样:

enter image description here

然而,我不希望标题降低其不透明度。我该如何实现这个目标?

以下是我的代码:

struct ContentView: View {
    @State private var selectionVariable = 0
    
    let sampleDict = [0: "Sample Title 1", 1: "Sample Title 2"]
    
    var body: some View {
        Menu {
            Picker(selection: $selectionVariable, label: Text("")) {
                ForEach(sampleDict.sorted(by: <), id: \.key) { base, name in
                    Text(name)
                }
            }
        }
        label: {
            Text("Eingaben im \(sampleDict[selectionVariable] ?? "")")
        }
    }
}
2个回答

2

看起来是一个错误。

一个可能的解决方法是使用.animation(nil)去除Picker动画效果:

struct ContentView: View {
    @State private var selectionVariable = 0

    let sampleDict = [0: "Sample Title 1", 1: "Sample Title 2"]

    var body: some View {
        Menu {
            Picker(selection: $selectionVariable, label: Text("")) {
                ForEach(sampleDict.sorted(by: <), id: \.key) { base, name in
                    Text(name)
                }
            }
        }
        label: {
            Text("Eingaben im \(sampleDict[selectionVariable] ?? "")")
        }
        .animation(nil) // add here
    }
}

2

我认为这看起来像是一个bug,无论如何都值得向苹果提交反馈。

这里是经过测试的解决方法(Xcode 12.1 / iOS 14.1)

    label: {
        Text("Eingaben im \(sampleDict[selectionVariable] ?? "")")
                .id(selectionVariable)    // << this one !!
    }

在 macOS(11.2.3)上,即使使用.id(),这个错误仍然存在。 - Joannes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接