Post

Replies

Boosts

Views

Activity

Reply to Keep ScrollView position when adding items on the top
i have same probelm with you but also solved when using .scrollPosition() .scrollTargetLayout() but sadly my app target is iOS15 is There AnyOther answer? import Foundation import SwiftUI struct TestChatUIView: View { @State var models: [TestChatModel2] = [] @State var modelId: String? init() { } // Identifier var body: some View { ZStack(alignment: .top) { ScrollViewReader { scrollViewProxy in ScrollView { LazyVStack(spacing: 0) { ForEach(models, id: \.id) { message in VStack { Text("\(message.id)") .flipped() .frame(minHeight: 40) // .onAppear(perform: { // // if message == models.first { // loadMore() // } else if message == models.last { // loadPrev() // } // }) } } } .scrollTargetLayout() } .scrollPosition(id: $modelId) .flipped() } } .safeAreaInset(edge: .bottom) { VStack { HStack { Button("Prepend") { loadPrev() } Button("Append") { loadMore() } } } .background(Material.ultraThin) } .onAppear(perform: { for int in 0...30 { models.insert(.init(id: "id \(models.count + 1)", date: Date()), at: 0) } }) } func loadMore() { for i in 0...20 { let newModel = TestChatModel2(id: "id \(models.count + 1)", date: Date()) models.insert(newModel, at: 0) } } func loadPrev() { for i in 0...20 { let newModel = TestChatModel2(id: "- id \(models.count + 1)", date: Date()) models.append(newModel) } } } struct TestChatModel2: Identifiable, Equatable { var id: String = UUID().uuidString var date: Date } extension View { func flipped() -> some View { self.rotationEffect(.radians(Double.pi)) .scaleEffect(x: -1, y: 1, anchor: .center) } } for exmaple my code is above it it works on iOS17 Obviously, but i must use ios15 so :( Needs Help!
Dec ’23