如何使用 SwiftUI 删除按钮的高亮显示?

Har*_*tel 3 swiftui

我有这个简单应用程序的按钮和标签。当我点击按钮时,它会突出显示我的按钮(您可以在附图中看到),那么我该如何禁用它?我错过了什么?

代码:

struct ContentView: View {
    @State private var displayLabel = 0
    var body: some View {
        GeometryReader{ geo in
            ZStack{
                Button(action: {
                    displayLabel += 1
                }, label: {
                    Rectangle()
                        .foregroundColor(.blue)
                        .frame(width: geo.size.width, height: geo.size.height)
                }).buttonStyle(PlainButtonStyle())
                
                Text("\(displayLabel)")
                    .font(Font.system(size:75, design: .rounded))
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

paw*_*222 5

您可以创建一个ButtonStyle没有突出显示的自定义:

struct StaticButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
    }
}
Run Code Online (Sandbox Code Playgroud)
Button(action: {
    displayLabel += 1
}) {
    Rectangle()
        .foregroundColor(.blue)
        .frame(width: geo.size.width, height: geo.size.height)
}
.buttonStyle(StaticButtonStyle())
Run Code Online (Sandbox Code Playgroud)