[英]SwiftUI ButtonStyle - how to check if button is disabled or enabled?
[英]How to conform the ButtonStyle protocol in SwiftUI?
我想在 SwiftUI 中创建一个自定义按钮,我想在整个应用程序中重复使用它。 该按钮基本上只是一个可点击的图像,上面没有 label。 我考虑为它创建一个自定义 ButtonStyle。 不过,我在遵守 ButtonStyle 协议时遇到了问题,因为我不知道我应该在这里选择哪种类型。
我已经尝试some View
或只是View
for <#type>
但没有成功。
struct customButtonStyle: ButtonStyle {
typealias Body = <#type>
}
我在尝试使用View
或some View
时收到的错误消息是: Type 'customButtonStyle' does not conform to protocol 'ButtonStyle'
和 XCode 只是添加了这一行typealias Body = <#type>
再次。
非常感谢您的帮助。
您在makeBody
function 中定义自定义样式。 您可以使用configuration.isPressed
在按下按钮时以稍微不同的方式配置按钮。
struct MyButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
// all of the modifiers you want to apply in your custom style e.g.:
.foregroundColor(configuration.isPressed ? .red : .blue)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.