frame
后添加一个.contentShape(Rectangle())
。我相信有很多方法可以实现这个功能,但这是我制作大按钮并使整个区域可点击的方法:
Button(action: { someAction() }, label: {
Text("OK")
.frame(minWidth: 200)
.contentShape(Rectangle())
.padding()
})
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(5.0)
我有同样的问题。但我不想让扩展的叠加影响到其余布局。如果您将所有内容嵌入ZStack中,并在可交互对象之前放置一个矩形,则可以控制手势的大小,类似这样:
Rectangle().frame(width: screen.width, height: 300).opacity(0.001)
.layoutPriority(-1)
只需确保将透明度设置为接近零,这样你就看不到它,而布局优先级设置为-1,这样它就不会影响视图。
ZStack {
Image(systemName: secured ? "eye.slash" : "eye")
.resizable()
.renderingMode(.template)
.aspectRatio(contentMode: .fit)
.frame(width: 16, height: 16)
Rectangle()
.frame(width: 22, height: 20)
.opacity(0.001)
.onTapGesture {
self.secured.toggle()
}
}
Button {
print("Tap")
} label: {
Text("Button Content")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.cornerRadius(8)
}
.padding()
修饰符。