【问题标题】:SwiftUI Button Action is not working but OnTapGestureSwiftUI 按钮操作不起作用,但 OnTapGesture
【发布时间】:2020-03-05 10:43:12
【问题描述】:

您好,我正在尝试实现简单的功能,但不知何故我无法使用按钮的 action,它从未触发过,不仅在此代码中,而且在我的整个应用程序中,我在做什么错误还是 SwiftUI 的故障。但是 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
                }
        }}}

帮我解决这个问题。

【问题讨论】:

  • 你的代码没问题,对我来说都可以在模拟器中使用。也许您还有其他问题。尝试在设备中运行?!
  • 我在模拟器上运行,我用的是测试版。
  • 重启模拟器对我有用。我的 Xcode 版本是 11.3.1 可能是模拟器中的一些小故障。

标签: ios swift uibutton swiftui action


【解决方案1】:

你需要在图片上设置一个尺寸

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-05
    • 2018-08-27
    • 1970-01-01
    相关资源
    最近更新 更多