Skip to content
View Switcher preview
Rendered from the SwiftUI source on this page.

View Switcher

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

A segmented layout toggle (list / grid / map) with an animated selection pill.

Inputs · SwiftUI · iOS 17

  • inputs
  • swiftui

The actual source

ViewSwitcherCard.swift
// View Switcher · syxUI · https://syxui.dev/components/sel-view-switcher
// 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)
    }
}

// Icon view-switcher - segmented layout toggle (list / grid / map)
struct ViewSwitcherCard: View {
    @State private var selected = 1
    let icons = ["list.bullet", "square.grid.2x2.fill", "map.fill"]
    var body: some View {
        VStack(alignment: .leading, spacing: 14) {
            HStack {
                Text("Layout").font(.system(size: 15, weight: .semibold))
                Spacer()
                Text("128 places").font(.system(size: 13)).foregroundStyle(.secondary)
            }
            HStack(spacing: 0) {
                ForEach(Array(icons.enumerated()), id: \.offset) { i, ic in
                    let on = selected == i
                    Image(systemName: ic)
                        .font(.system(size: 15, weight: .semibold))
                        .foregroundStyle(on ? Color.primary : Color.secondary)
                        .frame(maxWidth: .infinity).frame(height: 32)
                        .background(on ? Color(.systemBackground) : Color.clear, in: RoundedRectangle(cornerRadius: 8, style: .continuous))
                        .shadow(color: .black.opacity(on ? 0.10 : 0), radius: 3, y: 1)
                        .padding(3)
                        .contentShape(Rectangle())
                        .onTapGesture { withAnimation(.snappy) { selected = i } }
                }
            }
            .background(Color.primary.opacity(0.06), in: RoundedRectangle(cornerRadius: 11, style: .continuous))
        }.formaCard()
    }
}
iOS 17 · 1 dependencies

Dependencies

SwiftUI
  • formaCard

Requires iOS 17