Tob*_*aut 11 ios swift swiftui
我知道习俗是如何ButtonStyle运作的。但我确实想要一个完全可重用的自定义按钮。
又名带有文本和图标的按钮,应用了一些样式。
我知道如何使用带有文本属性的 ButtonStyle 来实现此目的,但我认为这完全是对按钮样式的滥用。
但我不想HStack在我的应用程序中复制具有大内容的按钮。
对于此类用例,您有什么建议?Button网上似乎没有人在 SwiftUI 中对 a 进行子类化。
ahe*_*eze 34
Button网上似乎没有人在 SwiftUI 中对 a 进行子类化。
真的。但这是因为它不可能 -Button是一个结构,所以它不能被子类化。相反,您可以做的是进行自定义View。
struct CustomButton: View {
var text: String
var icon: Image
var clicked: (() -> Void) /// use closure for callback
var body: some View {
Button(action: clicked) { /// call the closure here
HStack {
Text(text) /// your text
icon /// your icon image
}
.foregroundColor(Color.green)
.padding()
.background(Color(.secondarySystemBackground))
.cornerRadius(16)
}
}
}
struct ContentView: View {
var body: some View {
CustomButton(
text: "Custom Button",
icon: Image(systemName: "plus")
) {
print("Clicked!")
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
| 归档时间: |
|
| 查看次数: |
9093 次 |
| 最近记录: |