Skip to content
Size Selector preview
Rendered from the SwiftUI source on this page.

Size Selector

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

A size picker (S / M / L / XL) with a filled active state and a size-guide link.

Inputs · SwiftUI · iOS 17

  • inputs
  • swiftui

The actual source

SizeSelectorCard.swift
// Size Selector · syxUI · https://syxui.dev/components/sel-size
// 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)
    }
}

// Size selector - filled square on the chosen size (e-commerce)
struct SizeSelectorCard: View {
    @State private var selected = 1
    let sizes = ["S", "M", "L", "XL"]
    var body: some View {
        VStack(alignment: .leading, spacing: 14) {
            HStack {
                Text("Size").font(.system(size: 15, weight: .semibold))
                Spacer()
                Text("Size guide").font(.system(size: 13, weight: .medium)).foregroundStyle(Color(red: 0.0, green: 0.48, blue: 1.0))
            }
            HStack(spacing: 10) {
                ForEach(Array(sizes.enumerated()), id: \.offset) { i, s in
                    let on = selected == i
                    Text(s)
                        .font(.system(size: 15, weight: .semibold))
                        .foregroundStyle(on ? Color(.systemBackground) : Color.primary)
                        .frame(maxWidth: .infinity).frame(height: 48)
                        .background(on ? Color.primary : Color.primary.opacity(0.05), in: RoundedRectangle(cornerRadius: 12, style: .continuous))
                        .overlay(RoundedRectangle(cornerRadius: 12, style: .continuous).stroke(Color.primary.opacity(on ? 0 : 0.12), lineWidth: 1))
                        .contentShape(RoundedRectangle(cornerRadius: 12))
                        .onTapGesture { withAnimation(.snappy) { selected = i } }
                }
            }
        }.formaCard()
    }
}
iOS 17 · 1 dependencies

Dependencies

SwiftUI
  • formaCard

Requires iOS 17