Skip to content
Timetable Grid preview
Rendered from the SwiftUI source on this page.

Timetable Grid

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

A weekly timetable grid with colored session blocks.

Cards · SwiftUI · iOS 17

  • cards
  • swiftui

The actual source

TableSchedule.swift
// Timetable Grid · syxUI · https://syxui.dev/components/table-schedule
// 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 tblBlue = Color(red: 0.0, green: 0.47, blue: 1.0)

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)
    }
}

/// Weekly timetable grid - hours down the side, days across, colored session blocks.
struct TableSchedule: View {
    let times = ["9", "10", "11", "12"]
    let days = ["M", "T", "W", "T", "F"]
    let fills: [[Int]] = [[1, 0, 2, 0, 1], [0, 1, 0, 0, 0], [2, 0, 1, 2, 0], [0, 0, 0, 1, 0]]
    func color(_ c: Int) -> Color { c == 1 ? tblBlue : (c == 2 ? tblGreen : Color.clear) }
    var body: some View {
        VStack(spacing: 6) {
            HStack(spacing: 6) {
                Color.clear.frame(width: 22, height: 1)
                ForEach(Array(days.enumerated()), id: \.offset) { _, d in
                    Text(d).font(.system(size: 11, weight: .semibold)).foregroundStyle(.secondary).frame(maxWidth: .infinity)
                }
            }
            ForEach(Array(times.enumerated()), id: \.offset) { ri, t in
                HStack(spacing: 6) {
                    Text(t).font(.system(size: 11).monospacedDigit()).foregroundStyle(.secondary).frame(width: 22, alignment: .trailing)
                    ForEach(Array(fills[ri].enumerated()), id: \.offset) { _, c in
                        RoundedRectangle(cornerRadius: 5)
                            .fill(c == 0 ? Color.primary.opacity(0.05) : color(c).opacity(0.85))
                            .frame(maxWidth: .infinity).frame(height: 26)
                    }
                }
            }
        }
        .padding(14)
        .tblCard(300)
    }
}
iOS 17 · No dependencies

Dependencies

SwiftUI
No external dependencies

Requires iOS 17