1

I have a LazyVGrid with a layout count: 2 when in portrait, and court: 3 when in landscape, in a scrollview. I use a ternary to change the count. Problem is when I scroll down than select a cell, when the model slides up and I rotate, the view dismisses by itself. I also notice the scroll seems to be in a totally different location. Do I need to build this differently? Funny thing is it only happens at certain places down in the scrollview. Its not consistent. Sometimes it works fine then as I continue to scroll down it'll start to happen. If I don't change the layout count in portrait or landscape, it works fine. It seems change the count causes this.

struct Feed_View: View {

@EnvironmentObject var viewModel : Post_View_Model 
@Environment(\.verticalSizeClass) var sizeClass 
@FocusState private var isFocused: Bool

var body: some View {
    
    Color("BGColor").ignoresSafeArea() 
    
    ZStack {
        
        VStack (alignment: .center, spacing: 0) {
            
            //MARK: - NAVIGATION BAR
            
            NavBar_View() // Top Navigation bar
                .frame(maxHeight: 40, alignment: .center)
            
            //MARK: - SCROLL VIEW
            ScrollView (.vertical, showsIndicators: false) {
                
                
                //MARK: - FEED FILL
                
                let layout =  Array(repeating: GridItem(.flexible(), spacing: 10), count: sizeClass == .compact ? 3 : 2)
                
                LazyVGrid(columns: layout, spacing: 10) {

                    ForEach (viewModel.posts, id: \.self) { posts in
                        
                        Feed_Cell(postModel: posts)
                        
                    } //LOOP
                    
                } //LAZYV
                .padding(.horizontal, 10).padding(.vertical, 10)
                
            } //SCROLL
  
            
        } //V
        
    } //Z

}

}

  • You'd have to include a [mre] to truly debug this -- what you have here doesn't represent the issue. But, in general, you have to make sure that if you have a hierarchy that changes depending on the orientation that SwiftUI can reconcile the views and tell which are the same. Watching Demystifying SwiftUI from the 2021 WWDC might be useful. Also, completely unrelated, but it's Swift convention to use `camelCase` to name your variables and types, not `Snake_Case`. – jnpdx Dec 02 '21 at 02:12
  • @jnpdx is this the answer of above question :P – Akhtar Sep 01 '22 at 17:53
  • Lazy loading could be the reason. I had a LazyVStack those subviews presents fullScreenCover. When I rotate the screen, presented view dismisses. LazyVGrid can do the same I think – Bekir Onat Akin Mar 25 '23 at 08:03

0 Answers0