I would like to recreate the same effect than Pinterest:
Given that I'm new in Swift, I have three simple questions:
my two cents for highly customisable segmented, with colors and fonts.
struct ContentView: View {
@State private var segmentedSelection = 0
let titles = ["AA", "BB", "CC"]
let colors = [Color.red, .green, .white]
var body: some View {
VStack {
CustomSegmentedControl(segmentedSelection: $segmentedSelection, titles: titles, colors: colors)
Spacer()
Text("Hello, selection is " + titles[segmentedSelection] )
.padding()
}
}
}
struct CustomSegmentedControl: View {
@Binding var segmentedSelection : Int
var titles : [String]
let colors : [Color]
var body: some View {
VStack {
HStack{
ForEach(0 ..< titles.count, id: \.self){ (i: Int) in
Button(action: {
print(titles[i])
segmentedSelection = i
}, label: {
Text(titles[i])
.foregroundColor(colors[i])
.font(.system(size: i == segmentedSelection ? 22 : 16))
})
.frame(minWidth: 0, maxWidth: .infinity)
}
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.