如何设置 SwiftUI 按钮的自定义突出显示状态 [英] How to set custom highlighted state of SwiftUI Button
本文介绍了如何设置 SwiftUI 按钮的自定义突出显示状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个按钮.我想为突出显示状态设置自定义背景颜色.如何在 SwiftUI 中执行此操作?
I have a Button. I want to set custom background color for highlighted state. How can I do it in SwiftUI?
Button(action: signIn) {
Text("Sign In")
}
.padding(.all)
.background(Color.red)
.cornerRadius(16)
.foregroundColor(.white)
.font(Font.body.bold())
推荐答案
为 SwiftUI beta 5 更新
SwiftUI 实际上为此公开了一个 API:ButtonStyle
.
struct MyButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.padding()
.foregroundColor(.white)
.background(configuration.isPressed ? Color.red : Color.blue)
.cornerRadius(8.0)
}
}
// To use it
Button(action: {}) {
Text("Hello World")
}
.buttonStyle(MyButtonStyle())
这篇关于如何设置 SwiftUI 按钮的自定义突出显示状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文