struct CustomSegmentedView: View { @Binding var currentIndex: Int var selections: [String] }
init(_ currentIndex: Binding<Int>, selections: [String]) { self._currentIndex = currentIndex self.selections = selections }
var body: some View { VStack { Picker("", selection: $currentIndex) { ForEach(selections.indices, id: \.self) { index in Text(selections[index]) .tag(index) .foregroundColor(Color.blue) } } .pickerStyle(.segmented) } .padding() }
struct CustomSegmentedViewUse: View { @State private var selectedIndex = 0 private var selections = ["SwiftUI", "iOS", "UIKit", "Swift", "ML"] var body: some View { NavigationStack { VStack { CustomSegmentedView($selectedIndex, selections: selections) Text("Selected: \(selections[selectedIndex])") Spacer() } .navigationTitle("DevTechie") } } }
struct CustomSegmentedView: View { @Binding var currentIndex: Int var selections: [String] init(_ currentIndex: Binding<Int>, selections: [String]) { self._currentIndex = currentIndex self.selections = selections } var body: some View { VStack { Picker("", selection: $currentIndex) { ForEach(selections.indices, id: \.self) { index in Text(selections[index]) .tag(index) .foregroundColor(Color.blue) } } .pickerStyle(.segmented) .tint(.orange) } .padding() } }
UISegmentedControl.appearance().selectedSegmentTintColor = .orange UISegmentedControl.appearance().backgroundColor = UIColor(Color.orange.opacity(0.3)) UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor(Color.primary)], for: .selected) UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor(Color.secondary)], for: .normal)
struct CustomSegmentedView: View { @Binding var currentIndex: Int var selections: [String] init(_ currentIndex: Binding<Int>, selections: [String]) { self._currentIndex = currentIndex self.selections = selections UISegmentedControl.appearance().selectedSegmentTintColor = .orange UISegmentedControl.appearance().backgroundColor = UIColor(Color.orange.opacity(0.3)) UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor(Color.primary)], for: .selected) UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor(Color.secondary)], for: .normal) } var body: some View { VStack { Picker("", selection: $currentIndex) { ForEach(selections.indices, id: \.self) { index in Text(selections[index]) .tag(index) .foregroundColor(Color.blue) } } .pickerStyle(.segmented) .tint(.orange) } .padding() } }
With that we have reached the end of this article. Thank you once again for reading. Don’t forget to follow 😍. Also subscribe our newsletter at https://www.devtechie.com