首页 > 解决方案 > 为什么我的按钮只能在选定区域点击?

问题描述

我有一个按钮,但它似乎只能在选定区域中单击。我试过玩框架等,但无济于事。

            Button(action: {
                print("HELLO")
            }){
                Text("HELLO")
                    .foregroundColor(.red)
                    .font(.title)
                    .fontWeight(.light)
                    .frame(width: 300, height: 100, alignment: .center)
                    .background(Rectangle().stroke(Color.red, lineWidth: 1.0))
            }

标签: swiftui

解决方案


尝试添加 .contentShape 修饰符:

Button(action:, label:)
  .contentShape(RoundedRectangle(cornerRadius: 8))

推荐阅读