A minimal sliding SwiftUI toggle button with support for 2 or 3 states.
SlidingToggleButton_Animation.mp4
Add SlidingToggleButton to your project via Swift Package Manager:
- Open your project in Xcode
- Navigate to File > Add Package Dependencies
- Enter the following URL:
https://github.com/TahaTesser/SlidingToggleButton.git
import SlidingToggleButton
struct ContentView: View {
@State private var twoIconSelection = 0
@State private var threeIconSelection = 0
var body: some View {
VStack {
// 2-Icon Toggle
SlidingToggleButton(selection: $twoIconSelection) {
Image(systemName: "sun.max.fill")
Image(systemName: "moon.fill")
}
// 3-Icon Toggle
SlidingToggleButton(selection: $threeIconSelection) {
Image(systemName: "sun.max.fill")
Image(systemName: "circle.lefthalf.filled")
Image(systemName: "moon.fill")
}
}
}
}Selection values: 0 (start), 1 (center for 3-icon), 2 (end for 3-icon)
SlidingToggleButton(
selection: $selection,
size: 32,
padding: 12,
backgroundColor: .blue.opacity(0.3),
buttonBackgroundColor: .blue.opacity(0.6),
vertical: true
) {
Image(systemName: "sun.max.fill")
Image(systemName: "moon.fill")
}| Platform | Minimum Version |
|---|---|
| iOS | 17.0+ |
| macOS | 14.0+ |
Swift Version: 5.9+ (Swift 6 compatible)
| Metric | 2-Icon | 3-Icon |
|---|---|---|
| First Render | 0.48ms | 0.50ms |
| State Toggle | 0.03ms | 0.02ms |
| Memory | 96 bytes | 96 bytes |
Optimized for 60fps rendering. See Benchmark Results for details.
SlidingToggleButton is available under the MIT license. See the LICENSE for more info.