2

Is there a way to change the clickable area of a swiftui button? I'm adding some buttons over the area of detected text and want to click it. But with this code I get these results

    var body: some View {
    HStack {
        ZStack{
            GeometryReader { geometry in
                Image(nsImage: self.img!)
                    .resizable()
                    .scaledToFit()
                    .background(self.rectReader())
            }
            ForEach(self.rects) { rect in
                Button(action: {
                    buttonPressed()
                }) {
                    Rectangle()
                    .fill(Color.init(.sRGB, red: 1, green: 0, blue: 0, opacity: 0.2))
                    .frame(width: rect.width, height: rect.height)
                }
                .offset(x: rect.xAxis, y: rect.yAxis)
            }
        }

The clickable area is much larger than the rect I create.

enter image description here

3
  • 3
    Why don't add .onTapGesture directly to Recatangle()? Commented Jan 7, 2020 at 19:43
  • 1
    You're really blowing me off!!! I'll take your advise. Nevertheless I'm working with macos. Commented Jan 7, 2020 at 19:44
  • 1
    It worked. Again Thanks a lot Commented Jan 7, 2020 at 19:46

1 Answer 1

1

https://stackoverflow.com/a/58422956/1334703

import SwiftUI

struct BlueButtonStyle: ButtonStyle {
    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
            .foregroundColor(configuration.isPressed ? Color.blue : Color.white)
            .background(configuration.isPressed ? Color.white : Color.blue)
            .cornerRadius(6.0)
            .padding()
    }
}

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello World")
                .frame(maxWidth: .infinity, maxHeight: .infinity)

            Button(action: {
            }) {
                Text("Press")
                    .frame(maxWidth: 100, maxHeight: 24)
            }
            .buttonStyle(BlueButtonStyle())
        }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.