I am using segment control in my home page. When ever I am changing segment it is reloading scroll view. I am looking something like addChild or removeChild in UIKit option so that my scroll position will not change.
struct HomeListView:View {
@State private var favoriteColor = 0
var body: some View {
VStack {
Picker("What is your favorite color?", selection: $favoriteColor) {
Text("Red").tag(0)
Text("Green").tag(1)
Text("Blue").tag(2)
}
.pickerStyle(.segmented)
.padding(EdgeInsets(top: 8, leading: 8, bottom: 8, trailing: 8))
selectedView
}
.navigationBarTitleDisplayMode(.inline)
.toolbar{
ToolbarItem(placement: .navigationBarTrailing){
CustomNavigationBar()
}
ToolbarItem(placement: .navigationBarLeading){
Image(systemName: "magnifyingglass")
}
}
}
@ViewBuilder var selectedView: some View {
switch favoriteColor {
case 0:
HomeViewListing()
default:
MostPopularView()
}
}
}
struct HomeViewListing:View {
@StateObject private var viewModel = HomeViewModel()
var body: some View {
ScrollView {
LazyVStack {
ForEach(viewModel.storyItems) { item in
StoryCell(model: item)
}
}
}
.scrollIndicators(.hidden)
}
}