admin管理员组文章数量:1287856
I am having an issue with keeping the list view 'frozen' in place when new items are added if the user has scrolled down to view and item. Here is the code that I am currently using:
import SwiftUI
struct MessageList: View {
@ObservedObject var socketManager: WebSocketManager
@State var isUpdating: Bool = false
@State private var currentOffset: CGFloat = 0
@State private var currentScrollID: UUID?
@State private var timer: Timer? = nil
func simulateMessages(){
timer = Timer.scheduledTimer(withTimeInterval: 0.25, repeats: true) { _ in
let newItem = Message(content: "Hello", sentFlag: true)
socketManager.messages.append(newItem)
}
}
func stopSimulation(){
timer?.invalidate()
timer = nil
}
var body: some View {
Button{
if isUpdating{
stopSimulation()
} else {
simulateMessages()
}
isUpdating.toggle()
} label: {
Text(isUpdating ? "Stop" : "Update")
}
List(socketManager.messages.reversed(), id: \.uuid) { message in
messageView(message: message)
.swipeActions(edge: .trailing) {
DeleteButton(socketManager: socketManager, message: message)
}
.swipeActions(edge: .leading){
SaveButton(socketManager: socketManager, message: message)
}
.contextMenu{
SaveButton(socketManager: socketManager, message: message)
DeleteButton(socketManager: socketManager, message: message)
}
}
}
}
#Preview {
let socketManager = {
let manager = WebSocketManager()
manager.messages = [Message(content: "Hello, World! \nthis is a long message \nThis \nnew line\na\nb\nc\nd", sentFlag: true),
Message(content: "Hello, World! \nthis is a long message \nThis \nnew line\na\nb\nc\nd", sentFlag: false)]
return manager
}()
MessageList(socketManager: socketManager)
}
I experimented with this solution. However, this uses a lazyVStack and I would like to keep the list if possible for styling and the swipe actions.
本文标签: iosKeep scroll position when new items are added to top of the listStack Overflow
版权声明:本文标题:ios - Keep scroll position when new items are added to top of the list - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741320572a2372182.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论