【问题标题】:Button appears on top but not pressable按钮出现在顶部但不可按下
【发布时间】:2019-09-21 01:38:06
【问题描述】:
public struct Frontside: View
{
  @Binding public var kanatext: String

  public var body: some View
  {
    ZStack{
      RoundedRectangle(cornerRadius: 25, style: .continuous)
        .foregroundColor(Color.red)
        .frame(width: 160, height: 160)
        .zIndex(0)

      Text(self.kanatext)
        .font(.title)
        .fontWeight(.black)
        .padding(50)
        .zIndex(1)

      VStack {
        Spacer()
        HStack {

          Button(action: {
            print("button pressed")
          }) {
            Image(systemName: "xmark.circle")
              .font(.title)
          }
          .mask(Circle())
          .opacity(0.4)



          Button(action: {
            print("button pressed")
          }) {
            Image(systemName: "checkmark.circle")
              .font(.title)
          }
          .mask(Circle())
          .opacity(0.4)
        }
      }
      .zIndex(2)
    }
  }
}

在上面的代码 sn-p 中,我使用 Zstack 将闪存卡的不同部分、背景、文本以及正确/错误按钮分层。最上层是按钮,它们显示正确,但由于某种原因,它们实际上不可按下。

【问题讨论】:

  • 按钮周围有一些奇怪的行为。这可能是一个错误。我发现当标签是图像时,按钮是不可操作的。但是,当标签是文本或文本和图像时,它可以正常工作。这里显示了一些问题:[stackoverflow.com/questions/58001370/…
  • 谢谢你,我也意识到了,并决定停止使用按钮并用点击手势制作图像。

标签: swiftui


【解决方案1】:

您是否尝试过使用 onTapGesture?这可能与您使用 ZStack 的事实有关。

尝试类似:

Image(systemName: "checkmark.circle")
    .font(.title)
    .onTapGesture {
       print("button pressed")
    }

在这种情况下,不需要将图像包装在按钮中。 如果这不起作用,请将 onTapGesture 添加到您的 VStack 并使其为空。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多