๐ŸŽ iOS/SwiftUI

[SwiftUI] Custom Selectable Modal

JerryiOS 2023. 2. 16. 10:19

์ƒ๊น€์ƒˆ

๊ตฌํ˜„

struct SelectableModal: View {
    
    let title: String
    let selectCases: [String]
    let onSelect: (String) -> Void
    
    var body: some View {
        VStack(spacing: 0) {
            Spacer().frame(height: 26)
            Text(title)
                .font(.system(size: 17, weight: .bold))
                .padding(.bottom, 16)
            Rectangle()
                .foregroundColor(ColorManager.black150)
                .frame(height: 1)
            ForEach(selectCases, id: \\.self) { selectCase in
                Button {
                    onSelect(selectCase)
                } label: {
                    VStack(spacing: 0) {
                        Rectangle()
                            .foregroundColor(ColorManager.black50)
                            .frame(height: 1)
                        Spacer()
                        Text(selectCase)
                            .foregroundColor(ColorManager.black600)
                            .font(.system(size: 16))
                        Spacer()
                    }
                    .frame(height: 52)
                }
            }
        }
        .frame(width: 310)
        .cornerRadius(24)
    }
}

์‚ฌ์šฉ

SelectableModal(
    title: "์‹ ๊ณ  ์‚ฌ์œ ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”",
    selectCases: ReportType.allCases.compactMap({  $0.rawValue})
) { selectedReport in
    // TODO: ๋ทฐ๋ชจ๋ธ์—์„œ report ํ•˜๊ธฐ
    viewModel.report(selectedReport: selectedReport)
}
๋ฐ˜์‘ํ˜•