ios 更改浏览器样式

bkkx9g8r  于 2023-10-21  发布在  iOS
关注(0)|答案(1)|浏览(173)

我正在从this GitHub repo对Xcode项目进行一些更改。我试图改变选择菜单的分段风格,但我无法让它工作。我想改变的选择器部分在下面。有人能帮帮我吗?

import SwiftUI
import VanMoofKit

// MARK: - SpeedLimitCard

extension BikeView {
    
    /// The SpeedLimitCard
    struct SpeedLimitCard {
        
        @EnvironmentObject
        private var bike: VanMoof.Bike
        
    }
    
}

// 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
                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(SegmentedPickerStyle())
            },
            actions: { currentSpeedLimit in
                ForEach(VanMoof.Bike.SpeedLimit.allCases, id: \.self) { speedLimit in
                    Button {
                        Task {
                            try await self.bike.set(speedLimit: speedLimit)
                        }
                    } label: {
                        Label {
                            Text(
                                verbatim: "\(speedLimit.localizedString) (\(speedLimit.measurement.formatted()))"
                            )
                        } icon: {
                            if currentSpeedLimit == speedLimit {
                                Image(systemName: "checkmark")
                            }
                        }
                    }
                }
            }
        )
        
    }
    
}

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

添加.pickerStyle(SegmentedPickerStyle()),我希望它更改为分段pickerstyle

l3zydbqr

l3zydbqr1#

在你提供的代码中没有Picker,所以设置.pickerStyle可能不会有任何效果。除非这是你正在使用的第三方库(我不熟悉)支持的东西。
下面是一个独立的例子,说明如何使用之前使用的选项来形成分段选择器:

struct ContentView: View {

    @State private var currentSpeedLimit = SpeedLimit.europe

    var body: some View {
        Picker("SpeedLimit", selection: $currentSpeedLimit) {
            ForEach(SpeedLimit.allCases, id: \.self) { speedLimit in
                Text(
                    verbatim: "\(speedLimit.localizedString) (\(speedLimit.measurement.formatted()))"
                )
            }
        }
        .pickerStyle(.segmented)
        .fixedSize() // stops it expanding more than necessary
        .padding()
        .onChange(of: currentSpeedLimit) { oldVal, newVal in
            // Task {
            //     try await self.bike.set(speedLimit: newVal)
            // }
            print("\(newVal) selected")
        }
    }
}

您可以尝试将此Picker插入到代码中,以替换使用参数actions传递到BikeView.CardForEach。祝你好运!

相关问题