Issue with Menu title and decreased opacity

a 夏天 提交于 2021-02-07 23:24:12

问题


I want to use the new Menu with SwiftUI but unfortunately there is one issue which I can't solve. After selecting one value of the Menu the Menutitle looks like so:

However I don't want the title to decrease it's opacity. How can I achieve this?

Here is my code:

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] ?? "")")
        }
    }
}

回答1:


I'd say it looks like a bug, anyway worth submitting feedback to Apple.

Here is tested workaround (Xcode 12.1 / iOS 14.1)

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



回答2:


It looks like a bug.

A possible workaround is to remove Picker animations using .animation(nil):

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


来源:https://stackoverflow.com/questions/64770871/issue-with-menu-title-and-decreased-opacity

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!