[英]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.