简体   繁体   English

SwiftUI 按钮操作不起作用,但 OnTapGesture

[英]SwiftUI Button Action is not working but OnTapGesture

Hello I am trying to achieve simple functionality but somehow I am unable to use action of button, it never triggered, and not only in this code but also in my entire application, Am I doing something wrong or is it a glitch from SwiftUI.您好,我正在尝试实现简单的功能,但不知何故我无法使用按钮的动作,它从未触发过,不仅在此代码中,而且在我的整个应用程序中,我是做错了什么还是 SwiftUI 的故障。 But onTapGesture works perfectly.但是onTapGesture工作得很好。

struct CounterView: View {
@State var counter = 0

var body: some View{

    HStack(alignment: .center, spacing: 8){
        Button(action: {
            //self.counter += 1
            }) {
            HStack {
                Image(systemName: "plus")
            }.padding(4.0)
            .overlay(
                RoundedRectangle(cornerRadius: 4.0)
                    .stroke(lineWidth: 1.0)
                .trim()
            )
                .onTapGesture {
                    self.counter += 1
                }
        }}}

Help me resolve this issue.帮我解决这个问题。

You need to set a size on the image您需要在图像上设置大小

Image(systemName: "plus")
    .font(.system(size: 40, weight: .regular))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM