ColorPicker

SwiftUI 有一个原生 ColorPicker 控件,允许用户选择颜色。点击该控件是展开一个大的视图来做选择。

// 要使用它,首先创建一个 @State 的 Color 属性
struct ContentView: View {
    @State private var bgColor = Color.red
    var body: some View {
        VStack {
            ColorPicker("Set the background color", selection: $bgColor)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(bgColor)
    }
}

// 默认情况下 ColorPicker 支持颜色选择中的不透明度,但您可以使用稍微不同的初始值设定项来禁用它:
ColorPicker("Set the background color", selection: $bgColor, supportsOpacity: false)

how-to-let-users-select-a-color-with-colorpicker-1~dark@2x.png