본문 바로가기
Language/Swift

SwiftUI를 이용한 뷰 우선순위 주기 (layoutPriority)

by IFLA 2022. 11. 11.

 

SwiftUI는 모든 사용 공간을 ContentView에 전달한다. ContentView는 자식 뷰에게 사용할 공간을 자식에게 제안하고 크기를 계산하도록 요청한다.

SwiftUI에서 모든 뷰는 우선 순위가 0이 기본 설정값이다. .layoutPriority()를 이용해서 뷰에 우선순위를 줄 수 있다.

 

기본코드

import SwiftUI

struct ContentView: View {
    var body: some View {
        HStack {
            Text("This is a moderately long string.")
                .font(.largeTitle)
                .border(Color.gray)

            Spacer()

            Text("This is a higher priority string.")
                .font(.largeTitle)
                .layoutPriority(1)
                .border(Color.gray)
        }
    }
}

 

실행화면

 


개발자 Document

Apple Developer Documentation

댓글


\