I am working on auto scroll in swiftUI by using Foreach. Its adding a divider automatically between view that can be seen into the give attached image below.
He is my complete code through you can reproduce the same issue. I have no idea why its showing the divider. I have tried with the following solution but it did’t work for Foreach Loop.
Remove top separator line in ListView
import SwiftUI
struct ColorsView: View {
@State private var timer: Timer?
@State private var offset: CGFloat = 0
@State private var closeCount = 0
let colors: [Color] = [
.red,
.blue,
.yellow,
.brown,
.green,
.orange,
.purple
]
var body: some View {
ZStack(alignment: .topLeading) {
GeometryReader { geometry in
autoScroller(geometry: geometry)
}
}
.ignoresSafeArea()
}
}
// MARK: Color change extension
extension ColorsView {
func autoScroller(geometry: GeometryProxy) -> some View {
ZStack {
ForEach(0..<100, id: .self) { index in
LinearGradient(gradient: Gradient(colors: [colors[index % colors.count], colors[(index + 1) % colors.count]]), startPoint: .top, endPoint: .bottom)
.offset(y: offset + CGFloat(index) * geometry.size.height)
.listRowInsets(.none)
}
}
.animation(.linear(duration: 3), value: offset)
.onAppear {
startColorChange(geometry: geometry)
}
.onDisappear {
stopColorChange()
}
}
func startColorChange(geometry: GeometryProxy) {
timer = Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { _ in
withAnimation {
offset -= geometry.size.height
if offset <= -geometry.size.height * CGFloat(colors.count) {
offset = 0
}
}
}
}
func stopColorChange() {
timer?.invalidate()
timer = nil
}
}
struct ColorsView_Previews: PreviewProvider {
static var previews: some View {
ColorsView()
}
}