小编Fle*_*hil的帖子

如何创建 .chartForegroundStyleScale 的动态输入

chartForegroundStyleScale在 Swift Charts 中,为每个数据系列设置 ShapeStyle 的签名是:

func chartForegroundStyleScale<DataValue, S>(_ mapping: KeyValuePairs<DataValue, S>) -> some View where DataValue : Plottable, S : ShapeStyle
Run Code Online (Sandbox Code Playgroud)

初始化程序KeyValuePairs( init(dictionaryLiteral: (Key, Value)...)) 仅采用可变参数,因此任何从数组初始化前景样式的尝试(在我的例子中<String, Color>)都会导致错误:

Cannot pass array of type '[(String, Color)]' as variadic arguments of type '(String, Color)'
Run Code Online (Sandbox Code Playgroud)

在我的应用程序中,图表系列的名称是根据数据动态设置的,因此尽管我可以生成[String : Color]字典或元组数组,(String, Color)但我看不到可以将其中任何一个传递到chartForegroundStyleScale? 除非我遗漏了一些东西,否则这似乎是 Swift 图表中的一个奇怪的限制,即系列名称需要为此修饰符进行硬编码?

swiftui swiftui-charts

8
推荐指数
2
解决办法
1802
查看次数

禁用 SwiftUI SegmentedPickerStyle Picker 中的分段?

我的 SwiftUI 应用程序有一个分段选择器,我希望能够根据从网络调用检索到的选项的可用性来禁用一个或多个选项。查看代码如下所示:

    @State private var profileMetricSelection: Int = 0
    private var profileMetrics: [RVStreamMetric] = [.speed, .heartRate, .cadence, .power, .altitude]
    @State private var metricDisabled = [true, true, true, true, true]

    var body: some View {
        VStack(alignment: .leading, spacing: 2.0) {
            ...(some views)...
            Picker(selection: $profileMetricSelection, label: Text("")) {
                ForEach(0 ..< profileMetrics.count) { index in
                    Text(self.profileMetrics[index].shortName).tag(index)
                }
            }.pickerStyle(SegmentedPickerStyle())
            ...(some more views)...
        }
    }

Run Code Online (Sandbox Code Playgroud)

我想要做的是metricDisabled根据网络数据修改数组,以便视图重绘以启用相关段。在 UIKit 中,这可以通过调用 UISegmentedControl 来完成setEnabled(_:forSegmentAt:),但我找不到使用 SwiftUI Picker 执行此操作的方法

我知道我可以诉诸将 UISegmentedControl 包装在 UIViewRepresentable 中,但在此之前我只是想检查我是否遗漏了一些东西......

uisegmentedcontrol swiftui

4
推荐指数
1
解决办法
2998
查看次数