我想在 SwiftUI 中创建一个我想在整个应用程序中重复使用的自定义按钮。该按钮基本上只是一个没有标签的可点击图像。我想为它创建一个自定义的 ButtonStyle 。但是,我在遵守 ButtonStyle 协议时遇到了问题,因为不知道我应该在这里选择哪种类型。
我已经尝试过some View
或只是View
为了<#type>
但没有成功。
struct customButtonStyle: ButtonStyle {
typealias Body = <#type>
}
Run Code Online (Sandbox Code Playgroud)
该错误消息我在尝试时使用View
或者some View
是:
Type 'customButtonStyle' does not conform to protocol 'ButtonStyle'
和XCode的只是增加了这条线typealias Body = <#type>
一次。
非常感谢您的帮助。
您在makeBody
函数中定义自定义样式。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)
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
915 次 |
最近记录: |