I'm trying to make one of my segments disable and configure different font color.
I'm able to achieve it by using .selectionDisabled(), which is only available in iOS 17 +.
I am wondering if there's an alternative for iOS 16.
Thank you
import SwiftUI
struct MySegment: Hashable {
let label: String
let isDisabled: Bool
}
struct MySegmentedControlView: View {
@State var selection: String = "item1"
let data: [MySegment] = [
MySegment(label: "item1", isDisabled: false),
MySegment(label: "item2", isDisabled: false),
MySegment(label: "item3", isDisabled: true)
]
init() {
UISegmentedControl.appearance().backgroundColor = .red.withAlphaComponent(0.15)
UISegmentedControl.appearance().selectedSegmentTintColor = .red
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.white], for: .selected)
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.gray], for: .normal)
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.systemGray6], for: .disabled)
}
var body: some View {
VStack {
Picker("", selection: $selection) {
ForEach(data, id: \.label) {
if #available(iOS 17.0, *) {
Text($0.label)
.selectionDisabled($0.isDisabled)
} else {
Text($0.label)
.tag($0.label)
.disabled($0.isDisabled)
}
}
}
.pickerStyle(.segmented)
}
.padding()
}
}