History
Loading...
Loading...
September 20, 2025
Picker with pickerStyle(.menu) for space-efficient selections. When you need to show many options without taking up screen real estate, wrap your picker in a menu style and it automatically creates a dropdown interface with native iOS styling and accessibility support.This approach creates reusable picker components with clear visual hierarchy, custom labeling, and selection indicators. The generic implementation allows type-safe usage across different data types while maintaining consistent UI patterns.
struct SmartPicker<T: Hashable & CustomStringConvertible>: View {
let title: String
let options: [T]
@Binding var selection: T
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Text(title)
.font(.caption)
.foregroundStyle(.secondary)
Picker(title, selection: $selection) {
ForEach(options, id: \.self) { option in
HStack {
Text(option.description)
Spacer()
if option == selection {
Image(systemName: "checkmark")
.foregroundStyle(.blue)
}
}
.tag(option)
}
}
.pickerStyle(.menu)
.buttonStyle(.bordered)
}
}
}
struct ContentView: View {
@State private var priority = Priority.medium
var body: some View {
SmartPicker(
title: "Priority Level",
options: Priority.allCases,
selection: $priority
)
.padding()
}
}
enum Priority: String, CaseIterable, CustomStringConvertible {
case low = "Low"
case medium = "Medium"
case high = "High"
var description: String { rawValue }
}Consistent picker styling improves user experience by providing clear visual feedback and reducing cognitive load. The reusable component pattern promotes code maintainability and ensures UI consistency across the app while leveraging SwiftUI's native accessibility features.