【问题标题】:SwiftUI: How can I increase the area in which a button can be triggered?SwiftUI:如何增加可以触发按钮的区域?
【发布时间】:2021-06-13 15:19:28
【问题描述】:

如何在不改变 UI 的情况下增加按钮的触发区域?

这是我的代码

struct ContentView: View {
    var body: some View {
        NavigationView {
            Text("Text")

            .navigationBarTitle(Text("Title"))
            .navigationBarItems(
                leading:
                Button(action: { print("add") }) {
                    Image(systemName: SFSymbolName.plus)
                        .font(.system(size: 18))
                }
            )
        }
    }
}

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    对于这种特殊情况,您可以为除前缘之外的所有边缘添加填充到按钮的label

    Button(action: { print("add") }) {
        Text("+")
            .padding(EdgeInsets(top: 20, leading: 0, bottom: 20, trailing: 50))
    }
    .background(Color.red) // This is just for seeing the hit area. You should get rid of it
    

    注意最大可点击区域应在标题上方的矩形内:

    【讨论】:

      【解决方案2】:

      Button 与它的内容一样大。在您的情况下,它与图像一样大。您的按钮或可点击区域很小,因为图像很小。

      您可以将您的图片包含在更大的frame 中,如下所示:

      Button(action: { print("add") }) {
          Image(systemName: SFSymbolName.plus)
              .font(.system(size: 18))
              .frame(width: 40, height: 40)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-15
        • 2012-06-20
        • 1970-01-01
        • 1970-01-01
        • 2019-11-09
        • 1970-01-01
        • 2021-06-04
        相关资源
        最近更新 更多