swift - 如何更改 SwiftUI 中分段选取器的颜色外观?

标签 swift swiftui uikit

有谁知道如何实现这个目标吗?我有红色、绿色、黄色和蓝色四种颜色,我希望我选择的片段具有指示颜色名称的背景/色调颜色。 这是我的代码:

let objectColors = Color.allCases

@State private var selectedColorIndex = 0

//declared inside body view

Picker("Colors", selection: $selectedColorIndex) {

        ForEach(0..<objectColors.count){ index in

            Text(self.objectColors[index].rawValue).tag(index)

        }

    }
    .pickerStyle(SegmentedPickerStyle())
    .padding(10)
    .onAppear {
        UISegmentedControl.appearance().selectedSegmentTintColor = UIColor.generateUIColor(colorIndex: selectedColorIndex)        
    }

这是我从中提取的列表

enum Color: String, CaseIterable {
    case red, yellow, green, blue
}

我尝试使用 onChange 或 onReceive(以及用于 subview 的合并的“Just()”)而不是 onAppear,但它们在 Playground 上崩溃并且无法在 Xcode 上运行。 我还在 UIAction 上看到了一个 WWDC 视频,我认为它对于更新 View 更改非常有用,但我不知道如何翻译它。请问有人有什么建议或帮助吗?谢谢

最佳答案

这里:由于您使用的是enum,因此所选类型也应该是enum类型,而且您的自定义enum命名也有误.


enter image description here

版本 1.0.0:

struct ContentView: View {
    
    @State private var selectedColor: ColorEnum = ColorEnum.blue
    
    @State private var renderView: Bool = Bool()

    var body: some View {
        
        Group {
            
            if renderView { SegmentedPickerView(selectedColor: $selectedColor) }
            else { SegmentedPickerView(selectedColor: $selectedColor) }
            
        }
        .onChange(of: selectedColor) { _ in renderView.toggle() }
        
    }
    
}

struct SegmentedPickerView: View {
    
    @Binding var selectedColor: ColorEnum

    init(selectedColor: Binding<ColorEnum>) {
        
        self._selectedColor = selectedColor
        
        UISegmentedControl.appearance().selectedSegmentTintColor = selectedColor.wrappedValue.ColorValue
        
    }

    var body: some View {
        
        VStack {
            
            Picker("", selection: $selectedColor) {
                
                ForEach(ColorEnum.allCases, id: \.self) { item in
                    
                    Text(item.rawValue)
                    
                }
                
            }
            .pickerStyle(SegmentedPickerStyle())

            Text("You selected: " + selectedColor.rawValue)
                .bold()
                .shadow(radius: 10.0)
                .padding()

        }
        .padding()
        .background(Color.black.opacity(0.1).cornerRadius(10.0))
        .foregroundColor(Color(selectedColor.ColorValue))
        .padding()
        
    }
}

enum ColorEnum: String, CaseIterable {
    
    case red, yellow, green, blue
    
    var ColorValue: UIColor {
        
        get {
            
            switch self {
            case .red: return UIColor.red
            case .yellow: return UIColor.yellow
            case .green: return UIColor.green
            case .blue: return UIColor.blue
            }
            
        }
        
    }
}

关于swift - 如何更改 SwiftUI 中分段选取器的颜色外观?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67096712/

相关文章:

ios - 支付你的钱 nsurl 错误空字符串

ios - Swift firebase .observe 或 .observeSingleEvent 不起作用

swift - 如何在 SwiftUI 中制作通用 PreferenceKey?

ios - 将 EnvironmentObject 传递给 ObservableObject 类

iphone - 支持 UIKit UIAppearance 协议(protocol)的类的最终列表

ios - 点在圆角矩形内吗?

ios - 确定 SpriteNode 何时离开屏幕?

SwiftUI - 在 iPad 上拖放重新排序可以在没有 EditMode 的情况下工作吗?

iphone - 旋转 UIPickerView,可能吗?

swift - 在 Swift 5 中使用嵌套字典过滤字典