SwiftUI 컨테이너 참조
스택, 그리드, 아웃라인 및 스크롤 개선 사항. iOS 14부터 iOS 26까지.
출처: WWDC 2020-10031, 2022-10056, 2023-10148, 2024-10144, 2025-256
빠른 결정
| 사용 사례 | 컨테이너 | iOS |
|---|---|---|
| 고정된 뷰 세로/가로 | VStack / HStack | 13+ |
| 겹치는 뷰 | ZStack | 13+ |
| 대형 스크롤 가능 리스트 | LazyVStack / LazyHStack | 14+ |
| 다중 열 그리드 | LazyVGrid | 14+ |
| 다중 행 그리드 (가로) | LazyHGrid | 14+ |
| 정적 그리드, 정밀 정렬 | Grid | 16+ |
| 계층적 데이터 (트리) | List with children: | 14+ |
| 사용자 정의 계층 구조 | OutlineGroup | 14+ |
| 내용 표시/숨기기 | DisclosureGroup | 14+ |
파트 1: 스택
VStack, HStack, ZStack
VStack(alignment: .leading, spacing: 12) {
Text("Title")
Text("Subtitle")
}
HStack(alignment: .top, spacing: 8) {
Image(systemName: "star")
Text("Rating")
}
ZStack(alignment: .bottomTrailing) {
Image("photo")
Badge()
}
ZStack 정렬: .center (기본값), .top, .bottom, .leading, .trailing, .topLeading, .topTrailing, .bottomLeading, .bottomTrailing
Spacer
HStack {
Text("Left")
Spacer()
Text("Right")
}
Spacer(minLength: 20) // 최소 크기
LazyVStack, LazyHStack (iOS 14+)
보일 때만 자식을 렌더링합니다. ScrollView 내부에서 사용하세요.
ScrollView {
LazyVStack(spacing: 0) {
ForEach(items) { item in
ItemRow(item: item)
}
}
}
고정된 섹션 헤더
ScrollView {
LazyVStack(pinnedViews: [.sectionHeaders]) {
ForEach(sections) { section in
Section(header: SectionHeader(section)) {
ForEach(section.items) { item in
ItemRow(item: item)
}
}
}
}
}
파트 2: 그리드
Grid (iOS 16+)
정밀한 정렬이 가능한 비지연 그리드. 모든 뷰를 한 번에 로드합니다.
Grid(alignment: .leading, horizontalSpacing: 10, verticalSpacing: 10) {
GridRow {
Text("Name")
TextField("Enter name", text: $name)
}
GridRow {
Text("Email")
TextField("Enter email", text: $email)
}
}
수정자:
gridCellColumns(_:)— 여러 열에 걸쳐 확장gridColumnAlignment(_:)— 열 정렬 재정의
Grid {
GridRow {
Text("Header").gridCellColumns(2)
}
GridRow {
Text("Left")
Text("Right").gridColumnAlignment(.trailing)
}
}
LazyVGrid (iOS 14+)
세로 스크롤 그리드. 열을 정의하고, 행은 무제한으로 늘어납니다.
let columns = [
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible())
]
ScrollView {
LazyVGrid(columns: columns, spacing: 16) {
ForEach(items) { item in
ItemCard(item: item)
}
}
}
LazyHGrid (iOS 14+)
가로 스크롤 그리드. 행을 정의하고, 열은 무제한으로 늘어납니다.
let rows = [GridItem(.fixed(100)), GridItem(.fixed(100))]
ScrollView(.horizontal) {
LazyHGrid(rows: rows, spacing: 16) {
ForEach(items) { item in
ItemCard(item: item)
}
}
}
GridItem.Size
| 크기 | 동작 |
|---|---|
.fixed(CGFloat) | 정확한 너비/높이 |
.flexible(minimum:maximum:) | 공간을 균등하게 채움 |
.adaptive(minimum:maximum:) | 맞는 만큼 생성 |
// Adaptive: 반응형 열 개수
let columns = [GridItem(.adaptive(minimum: 150))]
파트 3: 아웃라인
계층적 데이터를 가진 List (iOS 14+)
struct FileItem: Identifiable {
let id = UUID()
var name: String
var children: [FileItem]? // nil = 리프 노드
}
List(files, children: \.children) { file in
Label(file.name, systemImage: file.children != nil ? "folder" : "doc")
}
.listStyle(.sidebar)
OutlineGroup (iOS 14+)
List 외부에서 사용자 정의 계층적 레이아웃을 위해.
List {
ForEach(canvases) { canvas in
Section(header: Text(canvas.name)) {
OutlineGroup(canvas.graphics, children: \.children) { graphic in
GraphicRow(graphic: graphic)
}
}
}
}
DisclosureGroup (iOS 14+)
@State private var isExpanded = false
DisclosureGroup("Advanced Options", isExpanded: $isExpanded) {
Toggle("Enable Feature", isOn: $feature)
Slider(value: $intensity)
}
파트 4: 일반적인 패턴
사진 그리드
let columns = [GridItem(.adaptive(minimum: 100), spacing: 2)]
ScrollView {
LazyVGrid(columns: columns, spacing: 2) {
ForEach(photos) { photo in
AsyncImage(url: photo.thumbnailURL) { image in
image.resizable().aspectRatio(1, contentMode: .fill)
} placeholder: { Color.gray }
.aspectRatio(1, contentMode: .fill)
.clipped()
}
}
}
가로 캐러셀
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: 16) {
ForEach(items) { item in
CarouselCard(item: item).frame(width: 280)
}
}
.padding(.horizontal)
}
파일 브라우저
List(selection: $selection) {
OutlineGroup(rootItems, children: \.children) { item in
Label {
Text(item.name)
} icon: {
Image(systemName: item.children != nil ? "folder.fill" : "doc.fill")
}
}
}
.listStyle(.sidebar)
파트 5: 성능
Lazy를 사용해야 할 때
| 크기 | 스크롤 가능? | 사용 |
|---|---|---|
| 1-20 | 아니오 | VStack/HStack |
| 1-20 | 예 | ScrollView 안의 VStack/HStack |
| 20-100 | 예 | LazyVStack/LazyHStack |
| 100+ | 예 | LazyVStack/LazyHStack 또는 List |
| 그리드 <50 | 아니오 | Grid |
| 그리드 50+ | 예 | LazyVGrid/LazyHGrid |
GridItem 배열 캐시 — body 외부에서 정의:
struct ContentView: View {
let columns = [GridItem(.adaptive(minimum: 150))] // ✅
var body: some View {
LazyVGrid(columns: columns) { ... }
}
}
iOS 26 성능
- 100k+ 항목에 대해 6배 빠른 리스트 로딩
- 16배 빠른 리스트 업데이트
- 스크롤 시 드롭된 프레임 감소
- 중첩된 ScrollView와 지연 스택이 이제 로딩을 적절히 지연:
ScrollView(.horizontal) {
LazyHStack {
ForEach(photoSets) { set in
ScrollView(.vertical) {
LazyVStack {
ForEach(set.photos) { PhotoView(photo: $0) }
}
}
}
}
}
파트 6: 스크롤 개선 사항
containerRelativeFrame (iOS 17+)
스크롤 컨테이너를 기준으로 뷰 크기 조정.
ScrollView(.horizontal) {
LazyHStack {
ForEach(cards) { card in
CardView(card: card)
.containerRelativeFrame(.horizontal, count: 3, span: 1, spacing: 16)
}
}
}
scrollTargetLayout (iOS 17+)
스냅 활성화.
ScrollView(.horizontal) {
LazyHStack {
ForEach(items) { ItemCard(item: $0) }
}
.scrollTargetLayout()
}
.scrollTargetBehavior(.viewAligned)
scrollPosition (iOS 17+)
가장 위에 보이는 항목 추적. 각 항목에 .id()가 필요합니다.
@State private var position: Item.ID?
ScrollView {
LazyVStack {
ForEach(items) { item in
ItemRow(item: item).id(item.id)
}
}
}
.scrollPosition(id: $position)
scrollTransition (iOS 17+)
.scrollTransition { content, phase in
content
.opacity(1 - abs(phase.value) * 0.5)
.scaleEffect(phase.isIdentity ? 1.0 : 0.75)
}
onScrollGeometryChange (iOS 18+)
.onScrollGeometryChange(for: Bool.self) { geo in
geo.contentOffset.y < geo.contentInsets.top
} action: { _, isTop in
showBackButton = !isTop
}
onScrollVisibilityChange (iOS 18+)
VideoPlayer(player: player)
.onScrollVisibilityChange(threshold: 0.2) { visible in
visible ? player.play() : player.pause()
}
리소스
WWDC: 2020-10031, 2022-10056, 2023-10148, 2024-10144, 2025-256
문서: /swiftui/lazyvstack, /swiftui/lazyvgrid, /swiftui/lazyhgrid, /swiftui/grid, /swiftui/outlinegroup, /swiftui/disclosuregroup
스킬: axiom-swiftui-layout, axiom-swiftui-layout-ref, axiom-swiftui-nav, axiom-swiftui-26-ref