iOS/SwiftUI

containerRelativeFrame

Hamp 2025. 10. 23. 13:27
반응형

containerRelativeFrame

 

🧩  역할

현재 뷰의 프레임을, 컨테이터를 기준으로, 일정한 칸(count)의로 나누고, 그 중
몇 칸(span)을 차지할 지 명시하여, 프레임을 설정한다.

 

 

📘 기본 문법

.containerRelativeFrame(
    _ axis: Axis, // 계산축
    count: Int, // 컨테이너 기준 몇 칸으로 나눌 지
    span: Int, // 차지할 칸 개수
    spacing: CGFloat = 0 // 칸 사이 간격
)

 

 

📏 예시

ScrollView(.horizontal) {
    LazyHStack(spacing: 10.0) {
        ForEach(viewModel.sections) { item in
            Rectangle()
                .fill(.purple)
                .aspectRatio(3.0 / 2.0, contentMode: .fit)
                .containerRelativeFrame(
                    .horizontal, count: 4, span: 1, spacing: .zero)
        }
    }
}

출처

https://developer.apple.com/documentation/SwiftUI/View/containerRelativeFrame(_:alignment:)

 

containerRelativeFrame(_:alignment:) | Apple Developer Documentation

Positions this view within an invisible frame with a size relative to the nearest container.

developer.apple.com

 

반응형