Skip to content
Days / Months preview
Rendered from the SwiftUI source on this page.

Days / Months

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

A segmented calendar with days & months.

Inputs · SwiftUI · iOS 17

  • inputs
  • swiftui

The actual source

PickerSegmented.swift
// Days / Months · syxUI · https://syxui.dev/components/picker-segmented
// Free in any project. Keep this line and credit syxUI where a person can read it.

import SwiftUI

let pkPurple = Color(red: 0.55, green: 0.40, blue: 0.90)

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

func weekdays() -> some View { HStack(spacing: 0) { ForEach(Array(["S", "M", "T", "W", "T", "F", "S"].enumerated()), id: \.offset) { _, d in Text(d).font(.system(size: 11, weight: .medium)).foregroundStyle(.secondary).frame(maxWidth: .infinity) } } }

func monthGrid(_ days: Int, _ lead: Int, _ selected: Set<Int>, _ accent: Color, range: Set<Int> = []) -> some View {
    LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 7), spacing: 6) {
        ForEach(Array(0..<lead), id: \.self) { _ in Color.clear.frame(height: 30) }
        ForEach(1...days, id: \.self) { d in
            Text("\(d)").font(.system(size: 13, weight: selected.contains(d) ? .bold : .regular)).foregroundStyle(selected.contains(d) ? .white : (range.contains(d) ? accent : .primary))
                .frame(width: 30, height: 30)
                .background { if selected.contains(d) { Circle().fill(accent) } else if range.contains(d) { Circle().fill(accent.opacity(0.14)) } }
        }
    }
}

struct PickerSegmented: View { var body: some View { VStack(spacing: 12) { HStack(spacing: 0) { ForEach(Array(["Days", "Months"].enumerated()), id: \.offset) { i, t in Text(t).font(.system(size: 14, weight: .medium)).foregroundStyle(i == 0 ? .primary : .secondary).frame(maxWidth: .infinity).padding(.vertical, 8).background(i == 0 ? Color(.systemBackground) : .clear, in: RoundedRectangle(cornerRadius: 8)).shadow(color: .black.opacity(i == 0 ? 0.06 : 0), radius: 2, y: 1).padding(2) }.background(Color.primary.opacity(0.06), in: RoundedRectangle(cornerRadius: 10)) }; weekdays(); monthGrid(31, 3, [1], pkPurple) }.formaCard().frame(width: 320) } }
iOS 17 · No dependencies

Dependencies

SwiftUI
No external dependencies

Requires iOS 17