You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:修改SwiftUI选择器为SegmentedPickerStyle未生效

解决SwiftUI分段选择器样式不生效的问题

问题根源

  1. 你把.pickerStyle(SegmentedPickerStyle())错误地加在了Gauge视图上——这个修饰符只对SwiftUI的Picker组件生效,Gauge根本不支持这个样式,所以完全起不到作用。
  2. 当前代码里的选择逻辑是用Button列表实现的自定义菜单,并非使用原生Picker组件,分段样式自然无法应用在这上面。

修复方案

需要替换原有的按钮列表为Picker组件,绑定当前选中的限速值,再正确应用分段样式,同时保留异步更新自行车限速的逻辑。

修改后的完整代码

import SwiftUI
import VanMoofKit

// MARK: - SpeedLimitCard

extension BikeView {
    /// The SpeedLimitCard
    struct SpeedLimitCard {
        @EnvironmentObject
        private var bike: VanMoof.Bike
        
        // 存储当前选中的限速,用于Picker绑定
        @State
        private var selectedSpeedLimit: VanMoof.Bike.SpeedLimit = .europe
    }
}

// MARK: - View

extension BikeView.SpeedLimitCard: View {
    /// The content and behavior of the view.
    var body: some View {
        BikeView.Card(
            placeholderValue: .europe,
            title: "Speed Limit",
            systemImage: "speedometer",
            provider: {
                try await self.bike.speedLimit
            },
            publisher: self.bike.speedLimitPublisher,
            content: { speedLimit in
                // 更新选中值为当前自行车的限速
                self.$selectedSpeedLimit.wrappedValue = speedLimit
                
                Gauge(
                    value: speedLimit.measurement.value,
                    in: 0...37
                ) {
                    Text(verbatim: speedLimit.measurement.unit.symbol)
                } currentValueLabel: {
                    Text(verbatim: .init(Int(speedLimit.measurement.value)))
                }
                .scaleEffect(1.4)
                .gaugeStyle(.accessoryCircular)
                .tint(.teal)
                // 移除错误添加的pickerStyle修饰符
            },
            actions: { _ in
                // 用Picker替换原有的Button列表
                Picker("Speed Limit", selection: $selectedSpeedLimit) {
                    ForEach(VanMoof.Bike.SpeedLimit.allCases, id: \.self) { speedLimit in
                        Text(verbatim: "\(speedLimit.localizedString) (\(speedLimit.measurement.formatted()))")
                            .tag(speedLimit)
                    }
                }
                .pickerStyle(SegmentedPickerStyle())
                .onChange(of: selectedSpeedLimit) { newSpeedLimit in
                    // 选中变化时异步设置自行车限速
                    Task {
                        try await self.bike.set(speedLimit: newSpeedLimit)
                    }
                }
            }
        )
    }
}

private extension VanMoof.Bike.SpeedLimit {
    var localizedString: String {
        switch self {
        case .europe:
            return "Europe"
        case .unitedStates:
            return "United States"
        case .japan:
            return "Japan"
        }
    }
}

关键修改说明

  • 移除了Gauge上无效的.pickerStyle修饰符。
  • 添加@State变量selectedSpeedLimit用于Picker的双向绑定,并通过content闭包里的当前限速值同步状态。
  • 把原有的Button列表替换为Picker,并应用SegmentedPickerStyle(),实现分段选择器样式。
  • 通过onChange监听Picker选中值的变化,异步调用自行车的限速设置方法。

内容的提问来源于stack exchange,提问作者TimTheBeastNL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 04:43:23