как сделать карточки с авто высотой в коллекции
Asked on 2025-09-02
1 search
To create cards with automatic height in a SwiftUI collection, you can utilize SwiftUI's flexible layout system. The session "Demystify SwiftUI containers" from WWDC 2024 provides insights into creating custom views and containers that can adapt to different content sizes.
In this session, they discuss using SwiftUI's ForEach
and other container views to manage collections of data. You can create a custom card view that adjusts its size based on its content by leveraging SwiftUI's layout system. Here's a basic approach:
-
Use a VStack or HStack: Depending on your layout, use a
VStack
orHStack
to stack your card elements vertically or horizontally. -
Flexible Layouts: Use SwiftUI's flexible layout modifiers like
frame
withminHeight
ormaxHeight
to allow the card to adjust its height based on its content. -
Dynamic Content: Ensure that the content within the card (like text or images) is set to adjust its size dynamically. For example, use
Text
with.lineLimit(nil)
to allow text to wrap and expand the card's height. -
Custom Containers: If you need more control, consider creating a custom container that uses SwiftUI's
GeometryReader
to dynamically adjust the size of its child views.
For more detailed guidance, you can refer to the Demystify SwiftUI containers session, particularly the "Composition" chapter, which starts at 03:17. This section discusses how to compose different kinds of content together, which is essential for creating flexible and adaptive UI components like cards with automatic height.

Demystify SwiftUI containers
Learn about the capabilities of SwiftUI container views and build a mental model for how subviews are managed by their containers. Leverage new APIs to build your own custom containers, create modifiers to customize container content, and give your containers that extra polish that helps your apps stand out.

Unlock the power of places with MapKit
Discover powerful new ways to integrate maps into your apps and websites with MapKit and MapKit JS. Learn how to save and reference unique places using Place ID. Check out improvements to search that make it more efficient to find relevant places. Get introduced to the new Place Card API that lets you display rich information about places so customers can explore destinations right in your app. And, we’ll show you quick ways to embed maps in your website with our simplified token provisioning and Web Embed API.

Say hello to the next generation of CarPlay design system
Explore the design system at the heart of the next generation of CarPlay that allows each automaker to express their vehicle’s character and brand. Learn how gauges, layouts, dynamic content, and more are deeply customizable and adaptable, allowing you to express your own design philosophy and create an iconic, tailored look. This session is intended for automakers, system developers, and anyone designing a system that supports the next generation of CarPlay.