ボタンの背景領域がユーザーの操作を検出しません。このボタンを操作する唯一の方法は、ボタンのテキスト/ラベル領域をタップすることです。ボタン全体をタップ可能にするにはどうすればよいですか?
struct ScheduleEditorButtonSwiftUIView: View {
@Binding var buttonTagForAction : ScheduleButtonType
@Binding var buttonTitle : String
@Binding var buttonBackgroundColor : Color
let buttonCornerRadius = CGFloat(12)
var body: some View {
Button(buttonTitle) {
buttonActionForTag(self.buttonTagForAction)
}.frame(minWidth: (UIScreen.main.bounds.size.width / 2) - 25, maxWidth: .infinity, minHeight: 44)
.buttonStyle(DefaultButtonStyle())
.lineLimit(2)
.multilineTextAlignment(.center)
.font(Font.subheadline.weight(.bold))
.foregroundColor(Color.white)
.border(Color("AppHighlightedColour"), width: 2)
.background(buttonBackgroundColor).opacity(0.8)
.tag(self.buttonTagForAction)
.padding([.leading,.trailing], 5)
.cornerRadius(buttonCornerRadius)
}
}
ベストアンサー1
適切な解決策は.contentShape()
API を使用することです。
Button(action: action) {
HStack {
Spacer()
Text("My button")
Spacer()
}
.contentShape(Rectangle())
}
提供された形状をボタンの形状に合わせて変更できます。ボタンが の場合はRoundedRectangle
、代わりに を提供できます。
編集:コメントで述べたように、.contentShape(Rectangle())
移動されました内部ボタンの閉鎖。そうしないと、SwiftUI の最新バージョンでは動作しません。