I am trying to scroll the image in tabbar, but i am unable to get the onchange when the image is scrolled.
@State private var currentIndex = 0
TabView(selection: $currentIndex) {
ForEach(model, id: \.self) { detail in
Image(uiImage: detail.image)
.resizable()
.ignoresSafeArea()
.frame(width: 100,height: 100,alignment: .center)
.aspectRatio(contentMode: .fill)
}
}
.onChange(of: currentIndex) { _ in
print("*********")
print(currentIndex)
}
XCode Version: 15.2
You need to change to
ForEach(0..<model.count) { i in
the complete code
import SwiftUI
struct ContentView: View {
@State private var currentIndex = 0
@State private var model: [Model] = []
var body: some View {
if model != [] {
TabView(selection: $currentIndex) {
ForEach(0..<model.count) { i in
Image(uiImage: model[i].image)
.resizable()
.ignoresSafeArea()
.frame(width: 100,height: 100,alignment: .center)
.aspectRatio(contentMode: .fill)
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
.onChange(of: currentIndex) { _ in
print("*********")
print(currentIndex)
}
} else {
Color.clear
.onAppear {
for _ in 0..<10 {
model.append(Model(image: UIImage(named: "test")!))
}
}
}
}
}
struct Model: Equatable, Identifiable {
let id = UUID()
var image: UIImage
}