I just started coding in SwiftUI and I am really excited with the new declarative way of building UI. I am trying to find a tidy way to organize all my components and reuse them consistently through my app (Use the same button in different views etc).
I can think of 2 ways to do it.
1) One would be to create a ButtonStyle
and the button label View
. Optionally you can use a token system.
Example
Using the button
struct ContentView: View {
static let capsuleButton = TokenButton(capsuleText: "Login")
static var body: some View {
Button(action: {}){
self.capsuleButton.buttonLabel
}.buttonStyle(self.capsuleButton.buttonStyle)
}
}
2) Second would be to create a costume View
for each button component and call that when you need it inside your other views
Example
The custom button View
struct MyButton: View {
var text: String
@Binding var isActive: Bool
var body: some View {
Button(action: {
self.isActive = true
}){
Text(text)
}
}
}
Using the button
struct ContentView: View {
@State var isActive = false
var body: some View {
MyButton(text: "Button", isActive: $isActive)
}
}
What is the best practice to use in SwiftUI?
I would really appreciate some feedback from other devs.
Aucun commentaire:
Enregistrer un commentaire