4

The default button style of ColorPicker is a circle as bellow.

enter image description here

I want to change the style of the circle button to Rectangle. But seems no API can change it style. So I put a Rectangle over it , and set it allowsHitTesting to false to transport click event to ColorPicker.

enter image description here

struct ColorPickerView: View {
    @State private var colorValue = Color.orange
    var body: some View {
        ZStack() {
            ColorPicker("", selection: $colorValue)
                .labelsHidden()
            Rectangle()
                .foregroundColor(.blue)
                .frame(width: 40, height: 40, alignment: .center)
                .allowsHitTesting(false)
        }
    }
}

But the ColorPicker did not present after click.

I put a circle bellow the Rectangle to test whether allowsHitTesting is useful. It can work properly responding to tap gesture to print "Circle tapped!".

struct ColorPickerView: View {
    @State private var colorValue = Color.orange
    var body: some View {
        ZStack() {
            ColorPicker("", selection: $colorValue)
                .labelsHidden()
            Rectangle()
                .foregroundColor(.blue)
                .frame(width: 40, height: 40, alignment: .center)
                .onTapGesture {
                    print("Circle tapped!")
                }
            Rectangle()
                .foregroundColor(.blue)
                .frame(width: 40, height: 40, alignment: .center)
                .allowsHitTesting(false)
        }
    }
}

Why the ColorPicker can not responding to tap gesture? Or Is there a way to customize the ColorPicker button?

swiftPunk
  • 7,543
  • 3
  • 7
  • 30
mars
  • 143
  • 9

1 Answers1

8

Simply use opacity, and send the ColorPicker to overlay, like in the code:


enter image description here

struct SquareColorPickerView: View {
    
    @Binding var colorValue: Color
    
    var body: some View {
        
        colorValue
            .frame(width: 40, height: 40, alignment: .center)
            .cornerRadius(10.0)
            .overlay(RoundedRectangle(cornerRadius: 10.0).stroke(Color.white, style: StrokeStyle(lineWidth: 5)))
            .padding(10)
            .background(AngularGradient(gradient: Gradient(colors: [.red,.yellow,.green,.blue,.purple,.pink]), center:.center).cornerRadius(20.0))
            .overlay(ColorPicker("", selection: $colorValue).labelsHidden().opacity(0.015))
            .shadow(radius: 5.0)

    }
}

Use case:

struct ContentView: View {
    
    @State private var colorValue = Color.orange
    
    var body: some View {

        SquareColorPickerView(colorValue: $colorValue)

    }
}
swiftPunk
  • 7,543
  • 3
  • 7
  • 30
  • 4
    How long have you used swiftui and how can I reach your level? – mars Apr 18 '21 at 05:27
  • 1
    There are much better than me here, I am just new here. but thanks. :) – swiftPunk Apr 18 '21 at 05:29
  • Here is another question of mine: https://stackoverflow.com/questions/66258627/ios-clock-animations-on-homescreen-widget. Can you help me see this question? – mars Apr 18 '21 at 07:32
  • widgets are a little tricky to work with them, I have never use it in my apps or projects, but you can find your answer with looking about other question and answers in this website. – swiftPunk Apr 18 '21 at 07:47