我正在从一个名为UIPillButton
. 下面是创建 SwiftUI 按钮的代码:
Button(action: {
print("Button tapped")
}) {
PillButton()
.padding(.top)
}
这是我创建 SwiftUI 视图的类,该视图PillButton
将我的自定义 UIButton 转换为:
struct PillButton: UIViewRepresentable {
var ntPillButton = NTPillButton(type: .filled, title: "Start Test")
func makeCoordinator() -> Coordinator { Coordinator(self) }
class Coordinator: NSObject {
var parent: PillButton
init(_ pillButton: PillButton) {
self.parent = pillButton
super.init()
}
}
func makeUIView(context: UIViewRepresentableContext<PillButton>) -> UIView {
let view = UIView()
view.addSubview(ntPillButton)
NSLayoutConstraint.activate([
ntPillButton.leadingAnchor.constraint(equalTo: view.leadingAnchor),
ntPillButton.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])
return view
}
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PillButton>) {}
}
问题是如果我点击按钮本身,按钮不会运行该操作PillButton
。仅当我选择PillButton
. 我怎样才能做到这一点,以便我可以将自定义PillButton
类用作普通的 SwiftUI 按钮?