Swift/Swift 개발 노트
[SwiftUI] 스크롤뷰 두개씩 배치하기
힛해
2024. 3. 31. 14:17
728x90
ScrollView(.horizontal) {
LazyHStack {
// 배열이 비어 있지 않은 경우에만 내부 로직 실행
if !items.isEmpty {
ForEach(0..<items.count, id: \.self) { index in
// 짝수 인덱스만 처리하여 두 개씩 그룹화
if index % 2 == 0 {
VStack {
// 현재 아이템
if items.indices.contains(index) {
NavigationLink(destination: UpdateBookMarkView(item: items[index])) {
HStack{
VStack(alignment: .leading){
Spacer()
HStack{
Text("\(items[index].cdTitle)").foregroundColor(.white).padding(.leading, 10).padding(.bottom,7).lineLimit(1).fontWeight(.semibold)
Spacer()
}
}
VStack(alignment : .trailing){
Text("사진").hidden()
Spacer()
}
}.frame(width : UIScreen.main.bounds.width * 0.43, height : 100).background( LinearGradient(colors: [gradientColors[index] ,gradientColors[index].opacity(0.9)], startPoint: .bottom, endPoint: .top)).cornerRadius(10)
}
}
// 다음 아이템 (있을 경우)
if items.indices.contains(index + 1) {
NavigationLink(destination: UpdateBookMarkView(item: items[index + 1])) {
HStack{
VStack(alignment: .leading){
Spacer()
HStack{
Text("\(items[index+1].cdTitle)").foregroundColor(.white).padding(.leading, 7).padding(.bottom,10).lineLimit(1)
Spacer()
}
}
VStack(alignment : .trailing){
Text("사진").hidden()
Spacer()
}
}.frame(width : UIScreen.main.bounds.width * 0.43, height : 100).background( LinearGradient(colors: [gradientColors[index+1] ,gradientColors[index+1].opacity(0.7)], startPoint: .bottom, endPoint: .top)).cornerRadius(10)
}
}else{
Spacer()
}
}.frame(maxWidth : UIScreen.main.bounds.width / 2 )
}
}