Skip to content

Instantly share code, notes, and snippets.

@HarshilShah
Created April 29, 2021 09:12
Show Gist options
  • Save HarshilShah/daa7643536ce46709eb6f10aeae455e4 to your computer and use it in GitHub Desktop.
Save HarshilShah/daa7643536ce46709eb6f10aeae455e4 to your computer and use it in GitHub Desktop.
A SwiftUI view that allows you to create a button which can be styled inline, based on whether it is pressed
import SwiftUI
struct TappableButton<Content: View>: View {
private struct PressableButtonStyle: ButtonStyle {
@ViewBuilder var label: (Bool) -> Content
func makeBody(configuration: Configuration) -> some View {
label(configuration.isPressed)
}
}
var action: () -> Void
@ViewBuilder var label: (Bool) -> Content
var body: some View {
Button(action: action) {
EmptyView()
}
.buttonStyle(PressableButtonStyle(label: label))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment