Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- calendar
- mvvm
- Observer
- viewlifecycle
- SWIFT
- NotificationCenter
- error
- self
- http
- Refresh
- Switch
- segue
- SWIFTUI
- singleton
- apns
- protocol
- escaping
- 고차함수
- 글또
- 화면전환
- class
- PushNotification
- list
- 회고
- array
- IOS
- ScrollView
- Git
- uikit
- struct
Archives
- Today
- Total
seong_hye, the developer
Swift) List 속 DropDown 사용하기 본문
📘List Drop Down
List 내 항목을 눌렀을 때 드롭다운처럼 하위 항목이 펼쳐지는 UI
Expandable List 또는 Accordion List라고 부름
각 항목의 펼침 상태를 추적하는 플래그가 필요함
🔹항목 모델 정의
struct DataModel {
let iconName: String
let title: String
var destination: AnyView
}
struct DropdownItem: Identifiable {
let id = UUID()
let title: TitleModel
let details: [DataModel]
var isExpanded: Bool = false
}
🔹메인 뷰 - List를 활용한 DropDown
@Binding var dropDownList: [DropdownItem]
var body: some View {
List {
ForEach(dropDownList.indices, id: \.self) { index in
Section {
Button(action: {
withAnimation {
dropDownList[index].isExpanded.toggle()
}
}) {
HStack {
Text(dropDownList[index].title.title)
Spacer()
Image(systemName: dropDownList[index].title.iconName)
.foregroundColor(.gray)
}
}
if dropDownList[index].isExpanded {
ForEach(dropDownList.indices, id: \.self) { detail in
HStack {
Text(dropDownList[index].details[detail].title)
Spacer()
Image(systemName: dropDownList[index].details[detail].iconName)
.foregroundColor(.gray)
}
}
}
}
}
}
.listStyle(InsetGroupedListStyle())
}
➡️ 설명
List를 통해 정의된 dropDownList를 받아
해당 index의 title이 선택되는 경우 isExpanded 값을 true로 변경시켜
아래의 detail list가 보이도록 함
➡️ 결과 화면
🔹메인 뷰 - 사용자 정의에 따른 DropDown
var icon: String
var title: String
var color: Color
var dropDownList: [DataModel]
@State private var showList = false
var body: some View {
VStack {
ZStack {
HStack {
Image(systemName: icon)
.frame(width: 30)
Text(title)
.font(.title2)
Spacer()
Image(systemName: "chevron.forward")
.font(.system(size: 15))
.foregroundStyle(.white)
.rotationEffect(.degrees(showList ? 90 : 0))
}
.bold()
.foregroundStyle(.white)
.padding()
.frame(maxWidth: .infinity)
.frame(height: 53)
.background(color, in: RoundedRectangle(cornerRadius: 15))
.onTapGesture {
withAnimation {
showList.toggle()
}
}
.zIndex(1)
ForEach(dropDownList.indices, id: \.self) { item in
NavigationLink {
dropDownList[item].destination
} label: {
HStack {
Image(systemName: dropDownList[item].iconName)
.frame(width: 30)
Text(dropDownList[item].title)
Spacer()
Image(systemName: "arrow.right")
}
.foregroundStyle(.white)
.frame(maxWidth: .infinity)
.padding()
.background(color.opacity(showList ? 1 :(1 - Double(item) * 0.3)), in: RoundedRectangle(cornerRadius: 15))
.padding(.leading, 15)
}
.offset(y: showList ? CGFloat(item * 58) : CGFloat(item * 8))
.scaleEffect(showList ? 1 : (1 - Double(item) * 0.04))
.zIndex(CGFloat(item * -1))
}
.offset(y: showList ? 58 : 0)
}
.padding(.horizontal)
Spacer()
}
.frame(height: showList ? CGFloat(dropDownList.count * 80) : 70)
}
➡️ 설명
사용자 정의에 따라 원하는 뷰를 하나 제작하여 보이도록 함
뷰를 클릭했을 경우 showList 값을 활용해 detail 값이 보이도록 함
배열로 받은 detail값의 index를 이용하여 뷰의 색상과 보이는 높이와 넓이를 조절
➡️ 결과 화면
참조 : https://www.youtube.com/watch?v=4kjPOw_pjx0
'IOS > SwiftUI' 카테고리의 다른 글
Swift) List 내용 삭제될 경우 기본 선택 값 변경되도록 제작하기 (0) | 2023.10.31 |
---|---|
SwiftUI) List에 CheckBox 연동하기 (0) | 2023.09.19 |
SwiftUI) Side Menu view에 대해 정리하기 (0) | 2022.12.06 |
Swift) Swift 스타일에 대해 알아보기 (0) | 2022.11.29 |
Swift) 배열 데이터 나누고 리플래시로 받아오기 (0) | 2022.11.16 |
Comments