Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Last active February 12, 2023 19:40
Show Gist options
  • Save sturdysturge/5282d1b640f4b049418d5331e0da5b47 to your computer and use it in GitHub Desktop.
Save sturdysturge/5282d1b640f4b049418d5331e0da5b47 to your computer and use it in GitHub Desktop.
import SwiftUI
struct SliderWithButtons<LeftButton, RightButton>: View
where LeftButton: View, RightButton: View {
@Binding var value: Double
let range: ClosedRange<Double>
let leftButton: () -> LeftButton
let rightButton: () -> RightButton
var body: some View {
HStack {
Button { withAnimation { value = range.lowerBound } } label: { leftButton() }
Slider(value: $value, in: range)
Button { withAnimation { value = range.upperBound } } label: { rightButton() }
}
}
}
@available(iOS 16.0, *)
struct ContentView: View {
@State var sliderValue = Double()
var body: some View {
SliderWithButtons(value: $sliderValue, range: 0...100) {
Text("L")
} rightButton: {
Text("H")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment