是否可以更改swiftui按钮的可点击区域? 我在检测到的文本区域上方添加了一些按钮,并希望单击它。 但是用这段代码我得到了这些结果
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)
}
}
可点击区域比我创建的矩形要大得多。
答案 0 :(得分:0)
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()
}
}