SwiftUI Navigation View - Handling Size Classes over multiple views

512 views Asked by At

I have the scenario where I intend on using a menu ('MenuView') that varies if the device is in portrait / landscape mode. I am using size classes to determine the device type and this successfully redraws the view on rotation. The menu uses navigation view/links to take you to a further view (the 'DetailedView'); this view also has differing views for portrait and landscape. Again I'm using size classes to successfully redraw the view based on the rotation.

However, what I find is that when I'm in the DetailedView and rotate the device, the display jumps straight back to the MenuView as, of course, it has recognized the size class change and adjusted that view. I would like the display to remain in this view.

How can I prevent the app from jumping to the 'MenuView' when I rotate the device that is displaying the 'DetailedView'? Code from the MenuView below:

Note: I'm using the StackNavigationViewStyle in this instance.

Any help would be gratefully received, thanks in advance!

struct MenuView: View {
    
    @Environment(\.horizontalSizeClass) var horizontalSizeClass: UserInterfaceSizeClass?
    @Environment(\.verticalSizeClass) var verticalSizeClass: UserInterfaceSizeClass?
    
    var body: some View {
        
        NavigationView {
            
            Color("MainBg")
                .edgesIgnoringSafeArea(.all)
            
            if horizontalSizeClass == .compact {
                PortraitMenuView()
            } else {
                LandscapeMenuView()
            }
            
        }.navigationViewStyle(StackNavigationViewStyle())
    }
}

Update: DetailedView - This is called from the PortraitMenuView and LandscapeMenuView. Included code from PortraitMenuView - the same navigation link is included in the LandscapeMenuView

struct DetailedView: View {
    
    @Environment(\.horizontalSizeClass) var sizeHClass
    @Environment(\.verticalSizeClass) var sizeVClass
    
    var formulae: Functions
    
    var body: some View {
        
        ZStack {
            Color.offWhite
                .edgesIgnoringSafeArea(.all)
            
            VStack {
                
                Group {
                    
                    if sizeHClass == .compact && sizeVClass == .compact {
                        DetailedViewLandscape()
                        
                    } else {
                        DetailedViewPortrait()
                        
                    }
                } 
            }       
        }  
    }
}
struct PortraitMenuView: View {
    
    var body: some View {
    
    VStack {
        ZStack {

            Circle()
            .fill(Color("kMainBg"))
            .frame(width: 300, height: 300)
            .overlay(
                    Text("FORMULA FINDER")
                        .font(.largeTitle).bold()
                        .multilineTextAlignment(.center)
                        .minimumScaleFactor(0.005)
                        .lineLimit(2)
                    .frame(width: 210, height: 210)
            )
        }
        
        Spacer()
           
        NavigationLink(destination: DetailedView()) {
            
            TileView(title: "Formulae", subtitle: "Functions and formulas", boxColor: Color.pastelGreen)
            
            
        }.offset(x: 40)
        
    }.padding(.bottom, 20)
      
    }
    
}
0

There are 0 answers