Skip to content
Striped Table preview
Rendered from the SwiftUI source on this page.

Striped Table

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

Zebra-striped rows with a signed change column.

Cards · SwiftUI · iOS 17

  • cards
  • swiftui

The actual source

TableStriped.swift
// Striped Table · syxUI · https://syxui.dev/components/table-striped
// Free in any project. Keep this line and credit syxUI where a person can read it.

import SwiftUI

let tblGreen = Color(red: 0.13, green: 0.62, blue: 0.32)

let tblRed = Color(red: 0.86, green: 0.24, blue: 0.21)

extension View {
    func tblCard(_ w: CGFloat) -> some View {
        self.frame(width: w)
            .background(Color(.systemBackground), in: RoundedRectangle(cornerRadius: 18, style: .continuous))
            .clipShape(RoundedRectangle(cornerRadius: 18, style: .continuous))
            .overlay(RoundedRectangle(cornerRadius: 18, style: .continuous).stroke(Color.primary.opacity(0.08), lineWidth: 1))
            .shadow(color: .black.opacity(0.04), radius: 10, x: 0, y: 4)
    }
}

/// Zebra-striped rows with a signed delta column (green up / red down).
struct TableStriped: View {
    let rows: [(String, String, String, Bool)] = [
        ("Jan", "1,240", "+12%", true), ("Feb", "1,890", "+52%", true),
        ("Mar", "1,410", "−25%", false), ("Apr", "2,100", "+49%", true), ("May", "2,640", "+26%", true),
    ]
    var body: some View {
        VStack(spacing: 0) {
            HStack {
                Text("Month").frame(maxWidth: .infinity, alignment: .leading)
                Text("Users").frame(width: 70, alignment: .trailing)
                Text("Change").frame(width: 60, alignment: .trailing)
            }
            .font(.system(size: 12, weight: .semibold)).foregroundStyle(.secondary)
            .padding(.horizontal, 16).padding(.vertical, 10)
            ForEach(Array(rows.enumerated()), id: \.offset) { i, r in
                HStack {
                    Text(r.0).font(.system(size: 14, weight: .medium)).frame(maxWidth: .infinity, alignment: .leading)
                    Text(r.1).font(.system(size: 14).monospacedDigit()).frame(width: 70, alignment: .trailing)
                    Text(r.2).font(.system(size: 13, weight: .medium).monospacedDigit()).foregroundStyle(r.3 ? tblGreen : tblRed).frame(width: 60, alignment: .trailing)
                }
                .padding(.horizontal, 16).padding(.vertical, 11)
                .background(i % 2 == 1 ? Color.primary.opacity(0.035) : Color.clear)
            }
        }
        .tblCard(320)
    }
}
iOS 17 · No dependencies

Dependencies

SwiftUI
No external dependencies

Requires iOS 17