Skip to content
Nutrition Facts preview
Rendered from the SwiftUI source on this page.

Nutrition Facts

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

A collapsible nutrition-facts panel.

Accordions · SwiftUI · iOS 17

  • accordion
  • swiftui

The actual source

AccNutrition.swift
// Nutrition Facts · syxUI · https://syxui.dev/components/acc-nutrition
// 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)
    }
}

// Nutrition facts accordion
struct AccNutrition: View {
    @State private var open = true
    let rows = [("Calories", "240"), ("Protein", "12g"), ("Carbs", "30g"), ("Fat", "8g")]
    let green = Color(red: 0.16, green: 0.66, blue: 0.40)
    var body: some View {
        VStack(spacing: 0) {
            HStack {
                Image(systemName: "leaf.fill").font(.system(size: 14)).foregroundStyle(green)
                Text("Nutrition Facts").font(.system(size: 15, weight: .semibold))
                Spacer()
                Image(systemName: "chevron.down").font(.system(size: 12, weight: .semibold)).foregroundStyle(.secondary).rotationEffect(.degrees(open ? 180 : 0))
            }.padding(.vertical, 13)
            if open {
                Divider().padding(.bottom, 6)
                ForEach(Array(rows.enumerated()), id: \.offset) { _, r in
                    HStack { Text(r.0).font(.system(size: 14)); Spacer(); Text(r.1).font(.system(size: 14, weight: .semibold)) }.padding(.vertical, 6)
                }
            }
        }.formaCard(16).frame(width: 300)
    }
}
iOS 17 · No dependencies

Dependencies

SwiftUI
No external dependencies

Requires iOS 17