Custom segmented picker for SwiftUI.
SwiftySegmentedPicker is available through Swift Package Manager. To install it, in Xcode 11.0 or later select File > Swift Packages > Add Package Dependency...
and add SwiftySegmentedPicker repository URL:
https://github.com/KazaiMazai/SwiftySegmentedPicker.git
struct SegmentedPickerExample: View {
let titles: [String]
@State var selectedIndex: Int?
var body: some View {
SegmentedPicker(
titles,
selectedIndex: Binding(
get: { selectedIndex },
set: { selectedIndex = $0 }),
selectionAlignment: .bottom,
content: { item, isSelected in
Text(item)
.foregroundColor(isSelected ? Color.black : Color.gray )
.padding(.horizontal, 16)
.padding(.vertical, 8)
},
selection: {
VStack(spacing: 0) {
Spacer()
Color.black.frame(height: 1)
}
})
.onAppear {
selectedIndex = 0
}
.animation(.easeInOut(duration: 0.3))
}
}
struct SegmentedPickerExample: View {
let titles: [String]
@State var selectedIndex: Int?
var body: some View {
SegmentedPicker(
titles,
selectedIndex: Binding(
get: { selectedIndex },
set: { selectedIndex = $0 }),
content: { item, isSelected in
Text(item)
.foregroundColor(isSelected ? Color.white : Color.gray )
.padding(.horizontal, 16)
.padding(.vertical, 8)
},
selection: {
Capsule()
.fill(Color.gray)
})
.onAppear {
selectedIndex = 0
}
.animation(.easeInOut(duration: 0.3))
}
}
SwiftySegmentedPicker is licensed under MIT license.