0

I am creating Twitter-like timeline view on SwiftUI. and I am trying to add timeline view of newly fetched tweets above the existing timeline just like any other Twitter client. but SwiftUI automatically sets scroll position to the top of added tweets. What I want it to do is just keep the position to the one of the existing tweets, and want user to scroll to the top.

I tried ScrollViewReader.scrollTo() after fetching tweets, but it gives the scroll slightly flickering on the screen, which is not what I want.

Here is the gist of my code below.

class TimelineViewModel: @ObservableObject {
  @Published tweets: [Tweet] = []
  
  func fetchTweets {
    // asynchronous function to fetch tweets
    DispatchQueue.main.async {
       self.tweets.append(fetchedTweets)
    }
  }

  func fetchMoreTweets {
    // asynchronous function to fetch tweets
    DispatchQueue.main.async {
       self.tweets.insert(fetchedMoreTweets, at: 0)
    }
}
@ObservedObject viewModel: TimelineViewModel

ScrollView {
  LazyVStack {
    ForEach(viewModel.tweets, id: \.id) { tweet in
      TweetView(tweet: tweet)
    }
  }
}

0 Answers0