본문 바로가기

Language/Swift45

SwiftUI를 이용한 뷰를 겹겹이 쌓기 (ZStack) VStack과 HStack은 세로나 가로로 뷰를 쌓을 때 이용한다. 간혹 뷰를 겹쳐서 쌓아야 할 때 ZStack 뷰를 이용해 뷰들을 겹겹이 쌓을 수 있다. 첫 번째에 입력한 내용이 먼저 그려진 다음 후속 뷰가 그 위에 계층화된다. 앱이 실행 되는 동안 어떠한 뷰를 다른 뷰 뒤로 밀거나 탭할 때 특정 뷰를 앞으로 가져올 때 .zIndex() 수정자를 이용한다. 기본 코드 struct ContentView: View { var body: some View { ZStack { Text("1") .background(Color.yellow) .foregroundColor(.red) .zIndex(1) Text("Photo credit: 김테스트") .padding(4) .background(Color.black.. 2022. 12. 8.
SwiftUI를 이용한 그리드 그리기 (LazyVGrid) Grid 뷰는 horizontal Direction 으로 사진을 배열하여 화면을 구성하기 쉽다. 기본 코드 struct ContentView: View { let data = Array(1...1000).map { "목록 \($0)"} let columns = [ GridItem(.adaptive(minimum: 100)) ] var body: some View { ScrollView { LazyVGrid(columns: columns, spacing: 20) { ForEach(data, id: \.self) { i in VStack { Capsule() .fill(Color.yellow) .frame(height: 50) Text(i) .forgroundColor(.secondary) } } } .pa.. 2022. 12. 5.
SwiftUI에서 GeometryProxy 사용하기 GeometryProxy GeometryReader 는 컨테이너 뷰의 한 종류이며, 다른 컨테이너뷰의 ViewBuilder와는 다르게 인자를 하나 받는데 그것이 GeometryProxy 객체다. 실제로 상위 뷰의 정보는 이 객체를 통해 접근이 가능하다. 기본적으로 GeometryProxy 는 size 프로퍼티를 갖고 있다. .frame(in: ) 라는 메소드가 있으며, 서브크립트도 지원한다. GeometryProxy 코드 struct GeometryProxy { var size: CGSize { get } var safeAreaInsets: EdgeInsets { get } func frame(in coordinateSpace: CoordinateSpace) -> CGRect subscrip(anchor.. 2022. 12. 1.
SwiftUI AspectRatio / GeometryReader 이용하기 AspectRatio 이미지를 원래 크기 그대로 넣고 싶은 경우도 있지만 사이즈 조정을 해서 지정된 사이즈 안에 이미지가 들어가게 하려면 AspectRatio 를 이용하면 원하는 크기에 이미지가 맞게 조정된다. .clipped() 를 이용해 이미지를 자를 수 있지만 .resizable() 과 .aspectRatio() 를 사용하면 간편하다. 기본 코드 import SwiftUI struct ContentView: View { var body: some View { VStack { Image(systemName: "star.circle") .resizable() .aspectRatio(contentMode: .fit) .frame(width: 300, height: 300) } } } .aspectRadi.. 2022. 11. 29.

\