Skip to content
Segmented Control preview
Rendered from the SwiftUI source on this page.

Segmented Control

From syxUI — written for both platforms, not translated between them.

An iOS-style sliding segmented control with an animated selection pill - Day / Week / Month.

Inputs · SwiftUI · iOS 17

  • inputs
  • swiftui

The actual source

SegmentedControlCard.swift
// Segmented Control · syxUI · https://syxui.dev/components/sel-segmented
// Free in any project. Keep this line and credit syxUI where a person can read it.

import SwiftUI

extension View {
    func formaCard(_ pad: CGFloat = 18) -> some View {
        self.padding(pad)
            .background(Color(.systemBackground), in: RoundedRectangle(cornerRadius: 20, style: .continuous))
            .overlay(RoundedRectangle(cornerRadius: 20, style: .continuous).stroke(Color.primary.opacity(0.06), lineWidth: 1))
            .shadow(color: .black.opacity(0.05), radius: 14, x: 0, y: 6)
    }
}

// iOS sliding segmented control (Day / Week / Month)
struct SegmentedControlCard: View {
    @State private var selected = 1
    let options = ["Day", "Week", "Month"]
    var body: some View {
        VStack(alignment: .leading, spacing: 14) {
            Text("Overview").font(.system(size: 15, weight: .semibold))
            GeometryReader { g in
                let w = g.size.width / CGFloat(options.count)
                ZStack(alignment: .leading) {
                    RoundedRectangle(cornerRadius: 8, style: .continuous)
                        .fill(Color(.systemBackground))
                        .shadow(color: .black.opacity(0.12), radius: 3, y: 1)
                        .padding(2).frame(width: w, height: 34)
                        .offset(x: CGFloat(selected) * w)
                    HStack(spacing: 0) {
                        ForEach(Array(options.enumerated()), id: \.offset) { i, opt in
                            Text(opt)
                                .font(.system(size: 13, weight: selected == i ? .semibold : .medium))
                                .foregroundStyle(selected == i ? Color.primary : Color.secondary)
                                .frame(width: w, height: 34)
                                .contentShape(Rectangle())
                                .onTapGesture { withAnimation(.snappy(duration: 0.22)) { selected = i } }
                        }
                    }
                }
            }
            .frame(height: 34)
            .background(Color.primary.opacity(0.06), in: RoundedRectangle(cornerRadius: 10, style: .continuous))
        }.formaCard()
    }
}
iOS 17 · 1 dependencies

Dependencies

SwiftUI
  • formaCard

Requires iOS 17