Hey. I´ve created an UIPickerView for selecting time durations in my App. Note: I used UIViewRepresentable to include this picker in my SwiftUI project, it shouldn't make any difference to the UIPickerView. Code:
struct DurationPickerView: UIViewRepresentable {
func makeUIView(context: Context) -> UIPickerView {
let picker = UIPickerView()
picker.dataSource = context.coordinator
picker.delegate = context.coordinator
picker.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
picker.setContentHuggingPriority(.defaultLow, for: .horizontal)
return picker
}
func updateUIView(_ uiView: UIViewType, context: Context) {}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UIPickerViewDelegate, UIPickerViewDataSource {
let parent: DurationPickerView
init(_ parent: DurationPickerView) {
self.parent = parent
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 6
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return 25
}
else if component == 1 {
return 1
}
else if component == 2 {
return 61
}
else if component == 3 {
return 1
}
else if component == 4 {
return 61
}
else if component == 5 {
return 1
}
return 0
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return "\(row)"
}
else if component == 1 {
return "Std."
}
else if component == 2 {
return "\(row)"
}
else if component == 3 {
return "Min."
}
else if component == 4 {
return "\(row)"
}
else if component == 5 {
return "Sek."
}
return ""
}
}
}
At the moment I'm using another pickerview row to describe the selectable rows. But thats not what I want... I want a row description that is not selectable and has a smaller text size for each selectable picker component like in the iPhone clock app:
I appreciate any help!